当前位置: 首页 > news >正文

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.17pip 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.
往期精彩

为了方便,我一口气将20多个python自动化相关的模块记录了下来。

python最好用的能源类可视化图表模块,没有之一!

python如何完成对 Excel文件的解密后读取?

http://www.lryc.cn/news/8483.html

相关文章:

  • HashMap如何避免内存泄露问题
  • crontab -e定时任务
  • JavaSE学习day7_01 面向对象
  • 有趣的HTML实例(十二) 早安、晚安动画(css+js)
  • 入行测试已经4年了 ,进华为后迷茫了3个月,做完这个项目我决定离职....
  • 【halcon】灰度直方图直观理解与应用
  • Android笔记:动画
  • Git学习总结
  • 第四天笔记
  • 《MySQL学习》 全局锁和表锁
  • Altium Designer输出生产文件Gerber、IPC、NC Drill、坐标文件--AD
  • 用VSCode搭建Vue.js开发环境及Vue.js第一个应用
  • Leetcode 每日一题 2341. 数组能形成多少数对
  • 前后端分离-小项目-3前后端交互
  • Spring如何整合MyBatis框架?使用XML及java类的配置方式
  • 第七届蓝桥杯省赛——8冰雹数(递归)
  • Android 10.0 设置静态ip重启后获取不到ip的修复
  • mysql笔记
  • 华为OD机试 - 最多等和不相交连续子序列(Python)| 真题+思路+考点+代码+岗位
  • 第四届宁波网安市赛训练题
  • Windows中MySQL 8.x版本忘记密码如何重设
  • 【信管12.1】信息文档管理与配置管理
  • 一文搞懂Linux的标准输出/错误重定向
  • 【OJ】计数的梦
  • 【项目实战】MySQL使用CONCAT字符串拼接函数实现与特殊字符的拼接
  • OpenCV实战(11)——形态学变换详解
  • SPI协议详解(Standard SPI、Dual SPI和Queued SPI)
  • 【代码随想录二刷】Day15-二叉树-C++
  • C++为什么能重夺年度语言?
  • 视频监控实时接入——以海康威视为例(2023.2.16)