赞
踩
PyMuPDF 和Fitz 是用于Python中处理PDF文件的相关模块。Fitz是P有MuPDF的字模块。提供一个简化和封装版本的P有MuPDF功能。
获取PDF的文档基本信息
# -*- coding: utf-8 -*-
# PyMuPDF==1.23.26
import fitz
pdf_path = 'rag_datas/text.pdf'
doc = fitz.open(pdf_path) # 文件加载
# basic PDF info
title = doc.metadata['title']
author= doc.metadata['author']# 文档作者
create_data= doc.metadata['creationDate'] # 文档创建时间
num_pages = doc.page_count # 文档页数
page = doc.load_page(0) # 第一页
page_height = page.bound().height
page_width = page.bound().width
获取pdf文档中的文本
# -*- coding: utf-8 -*-
# PyMuPDF==1.23.26
import fitz
pdf_path = 'rag_datas/text.pdf'
doc = fitz.open(pdf_path) # 文件加载
num_pages = doc.page_count # 文档页数
# Text info of pdf
for page_index in range(num_pages ):
page = doc.load_page(page_index)# 获取页面内容
text = page.get_text()# 获取页面文本
print(f"第{page_index + 1} 页的文本内容为:\n{text }\n")
获取pdf文档中的图片
# -*- coding: utf-8 -*-
# PyMuPDF==1.23.26
import fitz
pdf_path = 'rag_datas/text.pdf'
doc = fitz.open(pdf_path) # 文件加载
num_pages = doc.page_count # 文档页数
# Image info of pdf
for page_index in range(num_pages ):
page = doc.load_page(page_index)# 获取页面内容
image_list = page.get_images()# 获取页面图片
print(image_list) # 图片基本信息
for img in image_list:
xref = img[0]
pix = fitz.Pixmap(doc, xref)
print(pix.colorspace, '-->', fitz.csRGB)
img_path f'../output/image{page_index + 1}_{xref}.png'
pix.save(img_path )
获取pdf文档中的表格
# -*- coding: utf-8 -*-
# PyMuPDF==1.23.26
import fitz
pdf_path = 'rag_datas/text.pdf'
doc = fitz.open(pdf_path) # 文件加载
num_pages = doc.page_count # 文档页数
# tables info of pdf
for page_index in range(num_pages ):
page = doc.load_page(page_index)# 获取页面内容
tables = page.find_tables()# 获取页面表格
print(f"tables: "{tables})
# 提取的表格数据将会保存为csv格式文件
for i, table in enumerate(tables):
df = tables[0].to_pandas()
print(df.head())
df.to_csv(f"../output/table_pd_{page_index}_{i+1}.csv", index=False)
获取pdf 文档 分割
# -*- coding: utf-8 -*-
# PyMuPDF==1.23.26
import fitz
pdf_path = 'rag_datas/text.pdf'
doc = fitz.open(pdf_path) # 文件加载
num_pages = doc.page_count # 文档页数
# 构建输出文件名,以页数命名
#
for i in range(1, num_pages ):
print(f"i"{i}")
# 创建一个新的Document对象,包含当前页面
new_pdf = fitz.open()
new_pdf.insert_pdf(pdf_document. from_page=i-1, to_page=i)
# 保存单独的PDF文件
new_pdf.save(output_pdf.format(i))
new_pdf.close()
pdf_document.close()
借助大模型进行文档问答
# -*- coding: utf-8 -*-
# PyMuPDF==1.23.26
import os
import fitz
from openai import OpenAI
def get_pdf_content(pdf_path:str)-> str:
doc = fitz.open(pdf_path)
num_pages = doc.page_count
bg_content_list = []
#Full Text of PDF
for page_index in range(num_pages):
page = doc.load_page(page_index)
text = page.get_text()
bg_content_list.append(text)
return ''.join(bg_content_list)
def get_answer(pdf_content: str, query:str) -> str:
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
response = clinet.chat.completions.create(
model = "gpt-3.5-turbo",
messages=[
{"role":"system","content":"You are a helpful assistant."},
{"role":"user","content":"f"The full text of PDF file is : {pdf_content}"},
{"role":"user","content":query}
],
max_tokens=1000
)
answer = response.choices[0].message.content
return answer
if __name__=="__main__":
content = get_pdf_content("rag_datas/text.pdf")
query_1 = '蚂蚁集团发布的大模型叫什么?'
print(get_answer(pdf_content = content, query=query_1 ))
query_2 = '混元大模型是什么时候发布的?'
print(get_answer(pdf_content = content, query=query_2 ))
参考:
版面分析–PDF解析神器PyMuPDF
github: https://github.com/pymupdf/PyMuPDF
官方文档:https://pymupdf.readthedocs.io/en/latest/tutorial.html
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。