The following article is from GOGO数据 Author 是沐沐呀
专注于数据分析、Python、SQL、JAVA、C、Excel等知识圈。爬虫、数据分析、可视化、方法论,一条龙服务!
↑ 关注 + 星标 ,每天学Python新技能
后台回复【大礼包】送你Python自学大礼包
PDF文档遵循一定的规范,例如精确定位了每个字符出现在页面上的坐标、根据坐标绘制的各种形状(线、矩形、曲线等)。所以,用PDF格式传输和打印文档可以保证格式的一致性,不会像Word那样因为渲染引擎的不同而出现格式错乱、多页少页等问题。
pip install pdf2docxpdf2docx使用
from pdf2docx import Converterimport os
from pdf2docx import Converter
def pdf_docx():
# 获取当前工作目录
file_path = os.getcwd()
# 遍历所有文件
for file in os.listdir(file_path):
# 获取文件后缀
suff_name = os.path.splitext(file)[1]
# 过滤非pdf格式文件
if suff_name != '.pdf':
continue
# 获取文件名称
file_name = os.path.splitext(file)[0]
# pdf文件名称
pdf_name = os.getcwd() + '\\' + file
# 要转换的docx文件名称
docx_name = os.getcwd() + '\\' + file_name + '.docx'
# 加载pdf文档
cv = Converter(pdf_name)
cv.convert(docx_name)
cv.close()测试
我们准备的pdf文档有格式,有图片。先来进行测试
控制台信息打印如下,0.17秒完成了3页pdf->docx文件的转换
[INFO] Start to convert E:\Python\pycharm++\GOGO数据\卢本伟.pdf
[INFO] [1/4] Opening document...
[INFO] [2/4] Analyzing document...
[WARNING] Replace font "MicrosoftYaHeiUI" with "Times New Roman" due to lack of data.
Deprecation: 'getText' removed from class 'Page' after v1.19.0 - use 'get_text'.
Deprecation: 'getImageList' removed from class 'Page' after v1.19.0 - use 'get_images'.
Deprecation: 'getImageBbox' removed from class 'Page' after v1.19.0 - use 'get_image_bbox'.
Deprecation: 'getPNGData' removed from class 'Pixmap' after v1.19.0 - use 'tobytes'.
Deprecation: 'getDrawings' removed from class 'Page' after v1.19.0 - use 'get_drawings'.
Deprecation: 'getLinks' removed from class 'Page' after v1.19.0 - use 'get_links'.
Deprecation: 'getArea' removed from class 'Rect' after v1.19.0 - use 'get_area'.
[INFO] [3/4] Parsing pages...
[INFO] (1/3) Page 1
[INFO] (2/3) Page 2
[INFO] (3/3) Page 3
[INFO] [4/4] Creating pages...
[INFO] (1/3) Page 1
[INFO] (2/3) Page 2
[INFO] (3/3) Page 3
[INFO] Terminated in 0.17s.转换完成后的docx文件格式如下:
现在我们已经完成了pdf转word的操作,这样的局限性太大了,万一我的pc没有python环境怎么搞?
pip install pyinstaller 详细步骤
执行完毕会发现生成了 3 个文件夹
其中 dist 文件夹就有我们已经打包完成的 exe 文件。
3、双击 exe 就可以运行成功了。一键抓换pdf-word
够方便的吧~~
今天的分享就到此结束啦,