pdf2docx - pdf 提取内容转 docx

文章目录


一、关于 pdf2docx



  • 基于 PyMuPDF 提取文本、图片、矢量等原始数据
  • 基于规则解析章节、段落、表格、图片、文本等布局及样式
  • 基于 python-docx 创建Word文档


主要功能

  • 解析和创建页面布局
    • 页边距
    • 章节和分栏 (目前最多支持两栏布局)
    • 页眉和页脚 [TODO]
  • 解析和创建段落
    • OCR 文本 [TODO]
    • 水平(从左到右)或竖直(自底向上)方向文本
    • 字体样式例如字体、字号、粗/斜体、颜色
    • 文本样式例如高亮、下划线和删除线
    • 列表样式 [TODO]
    • 外部超链接
    • 段落水平对齐方式 (左/右/居中/分散对齐)及前后间距
  • 解析和创建图片
    • 内联图片
    • 灰度/RGB/CMYK等颜色空间图片
    • 带有透明通道图片
    • 浮动图片(衬于文字下方)
  • 解析和创建表格
    • 边框样式例如宽度和颜色
    • 单元格背景色
    • 合并单元格
    • 单元格垂直文本
    • 隐藏部分边框线的表格
    • 嵌套表格
  • 支持多进程转换

pdf2docx 同时解析出了表格内容和样式,因此也可以作为一个表格内容提取工具。


限制

  • 目前暂不支持扫描PDF文字识别
  • 仅支持从左向右书写的语言(因此不支持阿拉伯语)
  • 不支持旋转的文字
  • 基于规则的解析无法保证 100%还原PDF样式

二、安装

1、 PyPI

shell 复制代码
$ pip install pdf2docx

更新

shell 复制代码
$ pip install --upgrade pdf2docx

2、从remote安装

Install pdf2docx directly from the master branch:

shell 复制代码
$ pip install git+git://github.com/dothinking/pdf2docx.git@master --upgrade

注:这种方式 pdf2docx 的版本可能比 PYPI 高,没有发布


3、从源码安装

Clone or download pdf2docx, navigate to the root directory and run:

shell 复制代码
$ python setup.py install

或者,使用开发模式

shell 复制代码
$ python setup.py develop

4、卸载

shell 复制代码
$ pip uninstall pdf2docx

三、转化 PDF

我们可以使用 Converter 类, 或者包装的 parse() 方法,来转化所有/指定的 pdf 页面到 docx。

如果pdf文件包含大量页面,支持多线程处理。


例 1: convert all pages

python 复制代码
from pdf2docx import Converter

pdf_file = '/path/to/sample.pdf'
docx_file = 'path/to/sample.docx'

# convert pdf to docx
cv = Converter(pdf_file)
cv.convert(docx_file)      # all pages by default
cv.close()

或使用 parse 方法:

python 复制代码
from pdf2docx import parse

pdf_file = '/path/to/sample.pdf'
docx_file = 'path/to/sample.docx'

# convert pdf to docx
parse(pdf_file, docx_file)

例 2: 转换指定页面

  • 通过 start(如果省略,则从第一页开始)和 end (如果忽略,则到最后一页)指定页面范围:

    python 复制代码
    # convert from the second page to the end (by default)
    cv.convert(docx_file, start=1)
    
    # convert from the first page (by default) to the third (end=3, excluded)
    cv.convert(docx_file, end=3)
    
    # convert from the second page and the third
    cv.convert(docx_file, start=1, end=3)

  • 或者,通过 pages 参数设置单独的页面:

    python 复制代码
    # convert the first, third and 5th pages
    cv.convert(docx_file, pages=[0,2,4])

注:关于输入参数的详细描述 请参阅convert()


例 3: multi-Processing

使用默认CPU计数启用多处理:

python 复制代码
cv.convert(docx_file, multi_processing=True)

指定CPU个数:

python 复制代码
cv.convert(docx_file, multi_processing=True, cpu_count=4)

注:多线程 仅适用于由 startend 指定的连续页面。


例 4: 转换加密的pdf

提供 password 参数,打开和转换加密 pdf

python 复制代码
cv = Converter(pdf_file, password)
cv.convert(docx_file)
cv.close()

四、提取表格

python 复制代码
from pdf2docx import Converter

pdf_file = '/path/to/sample.pdf'

cv = Converter(pdf_file)
tables = cv.extract_tables(start=0, end=1)
cv.close()

for table in tables:
    print(table)

The output may look like:

python 复制代码
...
[['Input ', None, None, None, None, None],
['Description A ', 'mm ', '30.34 ', '35.30 ', '19.30 ', '80.21 '],
['Description B ', '1.00 ', '5.95 ', '6.16 ', '16.48 ', '48.81 '],
['Description C ', '1.00 ', '0.98 ', '0.94 ', '1.03 ', '0.32 '],
['Description D ', 'kg ', '0.84 ', '0.53 ', '0.52 ', '0.33 '],
['Description E ', '1.00 ', '0.15 ', None, None, None],
['Description F ', '1.00 ', '0.86 ', '0.37 ', '0.78 ', '0.01 ']]

五、命令行交互

shell 复制代码
$ pdf2docx --help

NAME
    pdf2docx - Command line interface for pdf2docx.

SYNOPSIS
    pdf2docx COMMAND | -

DESCRIPTION
    Command line interface for pdf2docx.

COMMANDS
    COMMAND is one of the following:

    convert
      Convert pdf file to docx file.

    debug
      Convert one PDF page and plot layout information for debugging.

    table
      Extract table content from pdf pages.

1、按页面范围

--start(如果省略,则从第一页开始)和--end(如果省略,则从最后一页)指定页面范围。

默认情况下,页面索引是基于零的,但可以通过--zero_based_index=False将其关闭,即第一个页面索引从1开始。


转换所有页面:

shell 复制代码
$ pdf2docx convert test.pdf test.docx

将页面从第二个转换到结尾:

shell 复制代码
$ pdf2docx convert test.pdf test.docx --start=1

将页面从第一个转换为第三个(index=2):

shell 复制代码
$ pdf2docx convert test.pdf test.docx --end=3

转换第二页和第三页:

shell 复制代码
$ pdf2docx convert test.pdf test.docx --start=1 --end=3

使用零基索引转换第一页和第二页,关闭:

shell 复制代码
$ pdf2docx convert test.pdf test.docx --start=1 --end=3 --zero_based_index=False

2、按页码

转换第一页、第三页和第五页:

shell 复制代码
$ pdf2docx convert test.pdf test.docx --pages=0,2,4

3、Multi-Processing

使用默认的CPU计数打开多处理:

shell 复制代码
$ pdf2docx convert test.pdf test.docx --multi_processing=True

指定CPU的计数:

shell 复制代码
$ pdf2docx convert test.pdf test.docx --multi_processing=True --cpu_count=4

六、图形界面

Thanks @JoHnTsIm providing a tkinter based user interface.

To launch the GUI:

shell 复制代码
$ pdf2docx gui


七、Technical Documentation

PDF文件遵循一定的格式规范,PyMuPDF 提供了便利的解析函数, 用于获取页面元素例如文本和形状及其位置。然后,基于元素间的相对位置关系解析内容,例如将"横纵线条 围绕着文本"解析为"表格",将"文本下方的一条横线"解析为"文本下划线"。最后,借助 python-docx 将解析结果重建为docx格式的Word文档。

以下分篇介绍提取PDF页面数据、解析和重建docx过程中的具体细节:


2024-07-19

相关推荐
小咖自动剪辑10 小时前
Base64与图片互转工具增强版:一键编码/解码,支持多格式
人工智能·pdf·word·媒体
五点钟科技12 小时前
Deepseek-OCR:《DeepSeek-OCR: Contexts Optical Compression》 论文要点解读
人工智能·llm·ocr·论文·大语言模型·deepseek·deepseek-ocr
开开心心_Every17 小时前
家常菜谱软件推荐:分类齐全无广告步骤详细
linux·运维·服务器·华为od·edge·pdf·华为云
开开心心就好18 小时前
键盘映射工具改键位,绿色版设置后重启生效
网络·windows·tcp/ip·pdf·计算机外设·电脑·excel
cuber膜拜18 小时前
Marp CLI快速入门
pdf·npm·markdown·ppt·marp
reasonsummer18 小时前
【办公类-53-09】20260131Python模仿制作2025学年第二学期校历(excel+pdf)
python·pdf
心语星光19 小时前
用python语言的pyautogui库实现伪批量将xdf文件打印为pdf文件
开发语言·python·pdf·自动化
iReachers19 小时前
PDF一机一码加密大师1.1.0更新至2026最新版, 强力加密PDF, 无需额外安装阅读器, 附CSDN下载地址
pdf
web打印社区1 天前
前端开发实现PDF打印需求:从基础方案到专业解决方案
前端·vue.js·react.js·electron·pdf
Kratzdisteln2 天前
【MCM】(x= (N_A),y=(N_G),z=(p))RGB=T*
pdf