Skip to main content

pdf-drawing-ocr-recognition: Pdor

定制项目,一个Python库,实现将PDF输入的指定模式的表格进行结构化读取(调用LLM OCR).

使用前准备

环境

  • Windows
  • Python 3.10+

依赖库

使用pip安装以下依赖库:

  • simpsave
  • PyPDF2
  • pdf2image
  • numpy
  • opencv-python (cv2)
  • pandas
  • toml
  • pyyaml
  • requests

快速指南

安装

项目已经发布在PyPi上,使用pip进行安装:

pip install pdor

库的名称简写为pdor.

然后,在你的项目中导入pdor:

import pdor

环境检查

提供了一个check_env()方法检测pdor是否可用.

示例:

import pdor

status, msg = pdor.check_env()
if not status: # 如果检查不通过
    for info in msg: # 逐项打印错误
        print(info)
  • status将是一个布尔值.如果为真,则环境可用.
  • msg是一个字符串列表,包括错误信息.

配置

Pdor的运行包含以下配置项:

  • max_try: LLM的最大重试次数
  • api_url: LLM的API地址
  • api_key: LLM的API KEY
  • llm_model: 使用的LLM视觉模型

对于每个配置项, 提供gettersetter进行配置.

仅当通过自检时可修改配置

Pdor模式

Pdor模式定义要识别的表格结构.每个模式都是一个PdorPattern实例.

读取模式

使用pattern.load方法读取预设的模式.接受一个参数,即待读取的模式名称.
目前预设了三种模式:

  • 700501-8615-72-12 750kV 第四串测控柜A+1端子排图左
  • 700501-8615-73-04 第四串W4Q1断路器LCP柜接线图二
  • duanzipai

示例:

import pdor
pattern = pdor.pattern.load('700501-8615-73-04 第四串W4Q1断路器LCP柜接线图二')

可以打印Pdor模式

自行构建和保存模式

可以自行实例PdorPattern创建模式.需要以下参数:

  • name: str 模式的名称
  • prompt: str 发送大模型的Prompt
  • dpi: int PDF转换为图片时的DPI
  • sub_imgs: list[list[float, float, float, float]] 子图列表.每个子图应该是一个表格.列表每一项都是一个由四个浮点数构成的子列表,分别对应子表位于原图的上,下,左,右百分比位置.如果子图列表为空,会自动生成一个包含整张图片的默认列表.

使用save方法进行存储.接受一个参数,即要写入的PdorPattern,名称和模式名称保持一致.

Pdor模式是只读的

Pdor单元

创建Pdor单元的实例以进行OCR解析.
实例化Pdor需要两个参数:

  • 构造的PDF文件路径
  • 构造的Pdor模式

可以打印Pdor单元格式化显示信息.Pdor单元是只读的.
调用parse()方法执行Pdor单元解析.可以通过开关print_repr关键字参数控制回显.
调用is_parsed()方法,可以判断是否已经解析.
在成功解析后,访问result属性即可获取结果.
其它可访问的属性包括:

  • file: 构造的PDF文件路径
  • pattern: 构造的Pdor模式
  • time_cost: 解析的用时

示例:

import pdor
from pdor import Pdor

# 构造单元
unit = Pdor("示例PDF.pdf", pdor.pattern.load("示例模式"))

# 打印单元信息
print(unit)

# 执行解析
unit.parse()

# 访问结果和解析耗时
print(f'结果: {unit.result}, 耗时: {unit.time_cost}')

结果

Pdor的结果存储在Pdor实例的result属性中.
仅当解析完毕后才可访问该属性.

输出结果

Pdor封装了常用的输出方式在Out类中.

支持的输出类型包括:

  • PLAIN_TEXT
  • MARKDOWN
  • SIMPSAVE
  • JSON
  • YAML
  • XML
  • TOML
  • PYTHON

默认输出为SIMPSAVE

Out是一个静态类,其中的静态方法out()自行输出.输出在构造单元的PDF同路径同名文件下.接受三个参数:

  • pdor: 待输出的Pdor单元
  • out_type: 输出的类型.封装在Out.TYPE枚举中
  • print_repr: 开关回显(关键字参数, 缺省值为True)

示例

import pdor

... # 假设我们前面获得了一个Pdor单元unit

Pdor.Out.out(unit, Pdor.Out.TYPE.SIMPSAVE) # 输出至simpsave ini
Pdor.Out.out(unit, Pdor.Out.TYPE.HTML, print_repr=False) # 输出至网页,并关闭回显  

Download files

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

Source Distribution

pdor-0.2.0.tar.gz (21.1 kB view details)

Uploaded Source

Built Distribution

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

pdor-0.2.0-py3-none-any.whl (21.8 kB view details)

Uploaded Python 3

File details

Details for the file pdor-0.2.0.tar.gz.

File metadata

  • Download URL: pdor-0.2.0.tar.gz
  • Upload date:
  • Size: 21.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.4

File hashes

Hashes for pdor-0.2.0.tar.gz
Algorithm Hash digest
SHA256 91a518b7aa209e9dbde3e7628b40a829ebdd01caf51cdcd5c459360bc27b978f
MD5 a1ebc513e68d7b4629160a255d78e1bf
BLAKE2b-256 dd1aaf7b2f2356382d9e828d054b4f05a0dca0d8d985d1b0249855d21dea4bb2

See more details on using hashes here.

File details

Details for the file pdor-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: pdor-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 21.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.4

File hashes

Hashes for pdor-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cd3e791fb660b0ad85533992c30f148799467658ab84ae02fc3c1db5dafbfd92
MD5 58fe7f2c9105e6d1f6ae2160e3247d9f
BLAKE2b-256 e0bc998bd9ac4a829c3d10452ea6f2bc30bdd2bb0c6f5d190fabed3a2facfc03

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 files

Supported by

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