可将PDF转为Word文件的Python库

《开源精选》是我们分享Github、Gitee等开源社区中优质项目的栏目,包括技术、学习、实用与各种有趣的内容。本期推荐的是一个可将PDF转为Word的Python库——pdf2docx。


主要功能

解析和创建页面布局

解析和创建段落

解析和创建图片

解析和创建表格

支持多进程转换

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


限制


示例

安装

$ pip install pdf2docx

更新

$ pip install --upgrade pdf2docx


我们可以使用 Converter类 或包装方法 parse() 将所有/指定的 pdf 页面转换为 docx。如果 pdf 文件包含大量页面,则支持批量处理。

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() 的方法:

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)

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

# 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:

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


-END-

开源协议:GPL-3.0

开源地址:https://github.com/dothinking/pdf2docx

展开阅读全文

页面更新:2024-05-01

标签:边框   段落   样式   表格   布局   文本   颜色   文字   页面   文件   图片

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2008-2024 All Rights Reserved. Powered By bs178.com 闽ICP备11008920号-3
闽公网安备35020302034844号

Top