文档API 参考📓 教程🧑‍🍳 食谱🤝 集成💜 Discord🎨 Studio
文档

XLSXToDocument

将 Excel 文件转换为文档。

pipeline 中的最常见位置预处理器 之前或索引管道的开始处
强制运行变量"sources": 文件路径或 ByteStream 对象
输出变量"documents": 文档列表
API 参考Converters (转换器)
GitHub 链接https://github.com/deepset-ai/haystack/blob/main/haystack/components/converters/xlsx.py

概述

该 XLSXToDocument 组件将 XLSX 文件转换为具有 CSV(默认)或 Markdown 格式的 Haystack 文档。它接收文件路径列表或 ByteStream 对象作为输入,并将转换结果输出为文档列表。可选地,您可以通过meta 输入参数为文档附加元数据。

要查看组件初始化时可以指定的其他参数,请查阅API 参考

用法

首先,安装 openpyxl 和 tabulate 包即可开始使用此转换器

pip install pandas openpyxl
pip install tabulate

单独使用

from haystack.components.converters import XLSXToDocument

converter = XLSXToDocument()
results = converter.run(sources=["sample.xlsx"], meta={"date_added": datetime.now().isoformat()})
documents = results["documents"]
print(documents[0].content)
# ",A,B\n1,col_a,col_b\n2,1.5,test\n"

在 pipeline 中

from haystack import Pipeline
from haystack.document_stores.in_memory import InMemoryDocumentStore
from haystack.components.converters import XLSXToDocument
from haystack.components.preprocessors import DocumentCleaner
from haystack.components.preprocessors import DocumentSplitter
from haystack.components.writers import DocumentWriter

document_store = InMemoryDocumentStore()

pipeline = Pipeline()
pipeline.add_component("converter", XLSXToDocument())
pipeline.add_component("cleaner", DocumentCleaner())
pipeline.add_component("splitter", DocumentSplitter(split_by="sentence", split_length=5))
pipeline.add_component("writer", DocumentWriter(document_store=document_store))
pipeline.connect("converter", "cleaner")
pipeline.connect("cleaner", "splitter")
pipeline.connect("splitter", "writer")

pipeline.run({"converter": {"sources": file_names}})