6.langchain 入门到放弃(二) Document loaders-Microsoft Office
langchain 入门到放弃(二) Document loaders-Microsoft Office
Load Microsoft Word
两种模式运行加载程序:“single”和“elements”。如果使用“single”模式,文档将作为单个langchain document对象返回。如果使用“元素”模式,非结构化库会将文档拆分为“标题”和“叙述文本”等元素。您可以在模式之后传入额外的非结构化kwargs,以应用不同的非结构化设置。
pip install python-docx
mode="single"
from langchain_community.document_loaders import UnstructuredWordDocumentLoader
word = UnstructuredWordDocumentLoader(file_path="../source/test.docx", mode="single", strategy="fast",encodings="utf-8")
wordData = word.load()
print(wordData)
输出
[Document(page_content='1、测试数据\n\n2、研发数据\n\n3、现场数据', metadata={'source': '../source/test.docx'})]
mode="elements"
from langchain_community.document_loaders import UnstructuredWordDocumentLoader
word = UnstructuredWordDocumentLoader(file_path="../source/test.docx", mode="elements", strategy="fast", encodings="utf-8")
wordData = word.load()
print(wordData)
输出
[Document(page_content='1、测试数据',
metadata={'source': '../source/test.docx', 'category_depth': 0, 'file_directory': '../source', 'filename': 'test.docx', 'last_modified': '2024-06-21T16:38:08', 'languages': ['zho'], 'filetype': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'category': 'Title'}),
Document(page_content='2、研发数据',
metadata={'source': '../source/test.docx', 'category_depth': 0, 'file_directory': '../source', 'filename': 'test.docx', 'last_modified': '2024-06-21T16:38:08', 'languages': ['zho'], 'filetype': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'category': 'Title'}),
Document(page_content='3、现场数据',
metadata={'source': '../source/test.docx', 'category_depth': 0, 'file_directory': '../source', 'filename': 'test.docx', 'last_modified': '2024-06-21T16:38:08', 'languages': ['zho'], 'filetype': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'category': 'Title'})]
Load Microsoft PowerPoint
pip install python-pptx
from langchain_community.document_loaders import UnstructuredPowerPointLoader
ppt = UnstructuredPowerPointLoader(file_path="../source/test.pptx", mode="single", strategy="fast",encodings="utf-8")
pptData = ppt.load()
print(pptData)
输出
[Document(page_content='研发方案\n\n6.2.0\n\n\n\n第三季\n\n06 完成对应功能开发\n\n07 进行技术预研', metadata={'source': '../source/test.pptx'})]
Load Microsoft Excel
pip install pandas
pip install openpyxl
非结构化加载程序,UnstructuredExcelLoader既可以用于“single”模式,也可以用于“elements”模式。如果在“elements”模式下使用加载程序,Excel文件中的每个工作表都将是一个非结构化表元素。如果在“single”模式下使用加载程序,则在文档元数据的“text_as_HTML”键中将提供该表的HTML表示。
from langchain_community.document_loaders import UnstructuredExcelLoader
excel = UnstructuredExcelLoader(file_path="../source/test.xlsx", mode="single", strategy="fast",encodings="utf-8")
excelData = excel.load()
print(excelData)
输出
[Document(page_content='\n\n\njava\nidea\n\n\npython\nideapy\n\n\nC\nvim\n\n\nC++\nvim\n\n\nC#\nvim\n\n\nphp\nidea\n\n\n', metadata={'source': '../source/test.xlsx'})]