使用LlamaParse解析和分析Excel电子表格
在本示例中,学习如何使用LlamaParse解析Excel电子表格,并(可选地)将其作为能够回答表格内数据问题的RAG应用程序的基础。
对于这个示例,我们将使用一个简单的DCF模板,您可以在此处下载。下载完成后,请将文件重命名为dcf_template.xlsx。

在本示例结束时,我们将使用LlamaIndex框架创建一个能够回答问题的迷你RAG应用,该应用使用OpenAI。您可以跳过此部分,或使用其他模型提供商
设置并连接到LlamaCloud
Section titled “Setup & Connect to LlamaCloud”要开始使用,我们将安装 llama-cloud-services 和(可选)llama-index:
!pip install llama-index!pip install llama-cloud-servicesimport osfrom getpass import getpass
os.environ["LLAMA_CLOUD_API_KEY"] = getpass("Llama Cloud API Key: ")os.environ["OPENAI_API_KEY"] = getpass("OpenAI API Key: ")一旦您获得了API密钥,就可以初始化 LlamaParse:
from llama_cloud_services import LlamaParse
parser = LlamaParse( parse_mode="parse_page_with_agent", model="openai-gpt-4o-mini", high_res_ocr=True, adaptive_long_table=True, outlined_table_extraction=True, output_tables_as_HTML=True,)设置好 LlamaParse 后,您可以解析示例电子表格,这将产生以下输出:
result = await parser.aparse("dcf_template.xlsx")llama_parse_documents = result.get_text_documents(split_by_page=True)
print(llama_parse_documents[1].text)Discounted Cash Flow Excel TemplateHere is a simple discounted cash flow excel template for estimating your company value based on this income valuation approachInstructions:1) Fill out the two assumptions in yellow highlight2) Fill in either the 5 year or 3 year weighted average figures in yellow highlightAssumptionsTax Rate 20%Discount Rate 15%5 Year Weighted Moving AverageIndication of Company Value $242,995.433 Year Weighted Moving AverageIndication of Company Value $158,651.07 5 Year Weighted Moving Average Past Years Forecasted Future Years Year 1 Year 2 Year 3 Year 4 Year 5 Year 6 Year 7 Year 8 Year 9 Year 10 Terminal ValuePre-tax income 50,000.00 55,000.00 45,000.00 52,000.00 60,000.00Income Taxes 10,000.00 11,000.00 9,000.00 10,400.00 12,000.00Net Income 40,000.00 44,000.00 36,000.00 41,600.00 48,000.00Depreciation Expense 5,000.00 4,000.00 3,000.00 2,000.00 1,000.00Capital Expenditures 10,000.00 8,000.00 5,000.00 5,000.00 7,000.00Debt Repayments 5,000.00 5,000.00 5,000.00 5,000.00 5,000.00Net Cash Flow 20,000.00 27,000.00 23,000.00 29,600.00 35,000.00 29,093.33 29,817.78 30,177.48 30,469.23 30,379.74 287,188.00Discounting Factor 0.8696 0.7561 0.6575 0.5718 0.4972 0.4972Present Value of Future Cash Flow 25,298.55 22,546.52 19,842.18 17,420.88 15,104.10 142,783.19 3 Year Weighted Moving Average Past Years Forecasted Future Years Year 1 Year 2 Year 3 Year 4 Year 5 Year 6 Terminal ValuePre-tax income 50,000.00 55,000.00 45,000.00Income Taxes 10,000.00 11,000.00 9,000.00Net Income 40,000.00 44,000.00 36,000.00Depreciation Expense 5,000.00 4,000.00 3,000.00Capital Expenditures 10,000.00 8,000.00 5,000.00Debt Repayments 5,000.00 5,000.00 5,000.00Net Cash Flow 20,000.00 27,000.00 23,000.00 23,833.33 24,083.33 23,819.44 158,253.59Discounting Factor 0.8696 0.7561 0.6575 0.6575Present Value of Future Cash Flow 20,724.64 18,210.46 15,661.67 104,054.30Notes:-We based this simple discounted cash flow excel model based on the weighted moving averages (5 year or 3 year) for simplicity, in case a constant growth rate cannot be easily determined.-The factors such as Depreciation Expense, Capital Expense and Debt Repayments remain constant, so consider this when looking at the forecasted figures.-For the terminal value constant growth rate, we make the assumption of the growth from the last forecasted year compared to the first forecasted year. Adjust in the formula as needed.现在,您可以创建一个能够基于解析后的Excel电子表格提供的上下文来回答问题的系统。在这一步中,我们将使用LlamaIndex。我们首先配置一个大型语言模型。在本例中,我们使用的是gpt-5:
from llama_index.llms.openai import OpenAI
llm = OpenAI(model="gpt-5-mini")最简单的方法是用解析后的电子表格内容来增强提示:
from llama_index.core.llms import ChatMessage
query_str = "Tell me about the income taxes in the past years (year 3-5) for the 5 year WMA table"context = "\n\n".join([doc.text for doc in llama_parse_documents])messages = [ ChatMessage( role="user", content=f"Here is some context\n<context>{context}</context>\n\nAnswer the following question: {query_str}", )]
response = await llm.achat(messages)print(response.message.content)In the 5‑year WMA table the income tax amounts for past years 3–5 are:- Year 3: $9,000.00- Year 4: $10,400.00- Year 5: $12,000.00
These equal 20% of the respective pre‑tax incomes (45,000; 52,000; 60,000), consistent with the 20% tax rate assumption. The taxes rise each year as pre‑tax income increases.