Skip to main content

Using large model to parse and translate PDF.

Project description

RecPDF

License: MIT Python Version

RecPDF是一个使用大模型解析和转换PDF文档的Python包,能够识别PDF中的文本、图片、表格、公式等元素,并将其转换为结构化的markdown格式。

功能特点

  • 📄 解析PDF文档中的文本、图片、表格、公式等元素
  • 🤖 支持使用各种大模型进行智能解析
  • 🔄 多线程并行处理,提高解析速度
  • 📝 输出结构化的markdown格式,保留原始文档的层次结构
  • 🎨 智能识别标题层级,优化文档结构
  • 🖼️ 自动处理图片和表格,保持文档的视觉完整性

安装

使用pip安装RecPDF:

pip install recpdf

依赖项

  • python-dotenv>=1.2.1
  • shapely>=2.1.2
  • langchain>=1.2.8
  • pymupdf>=1.26.7
  • langchain-openai>=1.1.7

快速开始

基本使用

from recpdf import parse_pdf

# 解析PDF文件
content, rect_images = parse_pdf(
    pdf_path="path/to/your/document.pdf",
    output_dir="./output",
    api_key="your_api_key",
    base_url="your_api_base_url",
    model="your_model_name",
    workers=2  # 多线程处理
)

print("解析完成,markdown内容已保存到指定目录")

使用环境变量

您也可以通过环境变量设置API相关参数,这样在调用函数时就不需要传递这些参数了:

  1. 创建一个 .env文件:
VLM_API_KEY=your_api_key
VLM_API_BASE=your_api_base_url
VLM_API_MODEL=your_model_name
  1. 然后在代码中加载环境变量:
import os
from dotenv import load_dotenv
from recpdf import parse_pdf

load_dotenv()

api_key = os.getenv('VLM_API_KEY')
base_url = os.getenv('VLM_API_BASE')
model = os.getenv('VLM_API_MODEL')

content, rect_images = parse_pdf(
    pdf_path="path/to/your/document.pdf",
    output_dir="./output",
    api_key=api_key,
    base_url=base_url,
    model=model
)

高级功能

调整Markdown结构

RecPDF还提供了一个 refine_markdown函数,可以进一步优化生成的markdown文档结构:

from recpdf.parser import refine_markdown

refined_content = refine_markdown(
    markdown_path="path/to/your/output.md",
    api_key="your_api_key",
    base_url="your_api_base_url",
    model="your_model_name"
)

print("Markdown结构优化完成")

工作原理

  1. PDF解析:使用PyMuPDF库提取PDF页面中的文本、图片和图形元素
  2. 区域识别:通过几何分析识别和合并页面中的内容区域
  3. 图像生成:将识别到的区域转换为高清图像
  4. 大模型解析:调用配置的大模型解析图像内容,识别文本、表格、公式等
  5. Markdown生成:将解析结果转换为结构化的markdown格式
  6. 可选优化:使用大模型进一步优化markdown文档结构

项目结构

recpdf/
├── __init__.py          # 包入口,导出主要函数
├── parser.py            # 核心解析功能实现
├── models.py            # 模型初始化模块
├── prompts.py           # 解析提示词定义
└── utils.py             # 工具函数

示例

项目提供了一些示例PDF文件和输出结果,位于 examples/目录中:

  • examples/test1.pdf - 简单文本PDF示例
  • examples/test2.pdf - 包含图片的PDF示例
  • examples/test3.pdf - 包含表格和公式的复杂PDF示例
  • examples/output/ - 解析结果输出目录

配置要求

  • Python 3.11或更高版本
  • 有效的大模型API密钥和访问地址
  • 支持视觉理解的大模型(如GPT-4V、Claude 3等)

许可证

本项目采用MIT许可证,详见LICENSE文件。

贡献

欢迎提交问题和拉取请求来改进这个项目!

联系方式

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

recpdf-0.1.0.tar.gz (10.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

recpdf-0.1.0-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

Details for the file recpdf-0.1.0.tar.gz.

File metadata

  • Download URL: recpdf-0.1.0.tar.gz
  • Upload date:
  • Size: 10.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for recpdf-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d69284606e6a67f9b3ac36e77b83f5011d1f62a883723520d2b7003e38f3b621
MD5 681cb61a8574094b6e69d2ceb336dfe2
BLAKE2b-256 35e88d0af242ad8773e461c1dbed0c5b4a76940f4c499cf95df58cb10d58d9b2

See more details on using hashes here.

Provenance

The following attestation bundles were made for recpdf-0.1.0.tar.gz:

Publisher: python-publish.yml on FreeCode001/RecPDF

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file recpdf-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: recpdf-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for recpdf-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 00704afee03f781576f697f1f16f575a561be3e9f4dbc9116802216acf422412
MD5 36f477774be1e9eef38f1eceef5f5b39
BLAKE2b-256 8fc453dc7b9fd70684655081c28f83be9fcea40f2caf40475366d66ca058a7f3

See more details on using hashes here.

Provenance

The following attestation bundles were made for recpdf-0.1.0-py3-none-any.whl:

Publisher: python-publish.yml on FreeCode001/RecPDF

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page