python如何使用最简单的方式将PDF转换成Word?

由于PDF的文件大多都是只读文件,有时候为了满足可以编辑的需要通常可以将PDF文件直接转换成Word文件进行操作。

看了网络上面的python转换PDF文件为Word的相关文章感觉都比较复杂,并且关于一些图表的使用还要进行特殊的处理。

本篇文章主要讲解关于如何使用python是实现将PDF转换成Word的业务过程,这次没有使用GUI应用的操作。

由于可能存在版本冲突的问题,这里将开发过程中需要使用的python非标准库的版本列举出来。

python内核版本:3.6.8
PyMuPDF版本:1.18.17
pdf2docx版本:0.5.1

可以选择pip的方式对使用到的python非标准库进行安装。

pip install PyMuPDF==1.18.17
pip install pdf2docx==0.5.1

完成上述的python依赖库安装以后,将pdf2docx导入到我们的代码块中。

# Importing the Converter class from the pdf2docx module.
from pdf2docx import Converter

然后,编写业务函数的代码块,新建一个pdfToWord函数来处理转换逻辑,主要就几行代码可以实现比较简单。

def pdfToWord(pdf_file_path=None, word_file_path=None):    
"""    
It takes a pdf file path and a word file path as input, and converts the pdf file to a word file.    
:param pdf_file_path: The path to the PDF file you want to convert    
:param word_file_path: The path to the word file that you want to create    
"""    
# Creating a Converter object.    
converter_ = Converter(pdf_file_path)    
# The `convert` method takes the path to the word file that you want to create, and the start and end pages of the PDF    
# file that you want to convert.    
converter_.convert(word_file_path, start=0, end=None)    
converter_.close()

最后,使用main函数调用pdfToWord函数可以直接完成文档格式的转换。

# A special variable in Python that evaluates to `True` if the module is being run directly by the Python interpreter, and
# `False` if it has been imported by another module.
if __name__ == '__main__':    
     pdfToWord('D:/test-data-work/test_pdf.pdf', 'D:/test-data-work/test_pdf.docx')
# Parsing Page 2: 2/5...Ignore Line "∑" due to overlap
# Ignore Line "∑" due to overlap
# Ignore Line "ç" due to overlap
# Ignore Line "A" due to overlap
# Ignore Line "i =1" due to overlap
# Ignore Line "æ" due to overlap
# Parsing Page 5: 5/5...
# Creating Page 5: 5/5...
# --------------------------------------------------
# Terminated in 3.2503201s.
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

奔跑的托马

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值