用python实现pdf转word_Python实现PDF转Word
”阅读此篇需要三分钟“
首先来看看来个PDF文件
我们来选择其中一个论文摘要:
使用我们的python代码转化后:
是不是很神奇?
现在网络上大部分的PDF转Word都是收费的,基本都是按页收费,有了我们的python代码后,我们就可以完全免费的将PDF转成Word了,这么好的福利我们赶紧来了解一下吧!
首先来看看我们要安装一些什么模块:
attrs==17.4.0
lxml==4.1.1
pdfminer3k==1.3.1
pluggy==0.6.0
ply==3.11
py==1.5.2
pytest==3.4.1
python-docx==0.8.6
six==1.11.0
使用pip模块管理工具即可安装。
如上图,将每个模块都安装好。
或者直接将模块放到requirements.txt文件里,运行
pip install -r requirements
安装即可
下一步就来开始coding了!
首先导入需要使用的模块:
import os
from io import StringIO
from io import open
from concurrent.futures import ProcessPoolExecutor
from pdfminer.pdfinterp import PDFResourceManager
from pdfminer.pdfinterp import process_pdf
from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
from docx import Document
然后定义好PDF文件的读取路径和Word文件的生成路径。
pdf_folder = r'/Users/wuyuqing/Desktop/Code/pdf2word/pdf'
word_folder = r'/Users/wuyuqing/Desktop/Code/pdf2word/word'
接下来我们定义使用的方法:
def read_from_pdf(file_path):
with open(file_path, 'rb') as file:
resource_manager = PDFResourceManager()
return_str = StringIO()
lap_params = LAParams()
device = TextConverter(
resource_manager,
return_str,
laparams=lap_params)
process_pdf(resource_manager, device, file)
device.close()
content = return_str.getvalue()
return_str.close()
return content
通过字节流的方式打开文件,读取内容。我们主要使用process_pdf这个函数处理pdf,详情处理步骤我们可以看看API是这么处理的(这API写好的代码,供参考,不需要你再次手写):
def process_pdf(rsrcmgr, device, fp, pagenos=None, maxpages=0, password='',
caching=True, check_extractable=True):
# Create a PDF parser object associated with the file object.
parser = PDFParser(fp)
# Create a PDF document object that stores the document structure.
doc = PDFDocument(caching=caching)
# Connect the parser and document objects.
parser.set_document(doc)
doc.set_parser(parser)
# Supply the document password for initialization.
# (If no password is set, give an empty string.)
doc.initialize(password)
# Check if the document allows text extraction. If not, abort.
if check_extractable and not doc.is_extractable:
raise PDFTextExtractionNotAllowed(
'Text extraction is not allowed: %r' % fp)
# Create a PDF interpreter object.
interpreter = PDFPageInterpreter(rsrcmgr, device)
# Process each page contained in the document.
for (pageno,page) in enumerate(doc.get_pages()):
if pagenos and (pageno not in pagenos): continue
interpreter.process_page(page)
if maxpages and maxpages <= pageno+1: break
下面我们考虑将字节流存成docx文档:
def save_text_to_word(content, file_path):
doc = Document()
for line in content.split('\n'):
paragraph = doc.add_paragraph()
paragraph.add_run(remove_control_characters(line))
doc.save(file_path)
# 将两个函数封装起来
def pdf_to_word(pdf_file_path, word_file_path):
content = read_from_pdf(pdf_file_path)
save_text_to_word(content, word_file_path)
主要功能完成,这样就算完工了
下面我们来调用读取pdf生成docx的方法
tasks = []
with ProcessPoolExecutor(max_workers=5) as executor:
for file in os.listdir(pdf_folder):
extension_name = os.path.splitext(file)[1]
if extension_name != '.pdf':
continue
file_name = os.path.splitext(file)[0]
pdf_file = pdf_folder + '/' + file
word_file = word_folder + '/' + file_name + '.docx'
print('正在处理: ', file)
result = executor.submit(pdf_to_word, pdf_file, word_file)
tasks.append(result)
while True:
exit_flag = True
for task in tasks:
if not task.done():
exit_flag = False
if exit_flag:
print('完成')
exit(0)
这样就可以生成doc文件了,怎么样是不是很简单?
用python实现pdf转word_Python实现PDF转Word相关推荐
- python读取pdf内容转word_Python实现PDF转Word
"阅读此篇需要三分钟" 首先来看看来个PDF文件 我们来选择其中一个论文摘要: 使用我们的python代码转化后: 是不是很神奇? 现在网络上大部分的PDF转Word都是收费的,基 ...
- python使用fpdf生成各种样式pdf表格数据
python使用fpdf生成各种样式pdf表格数据 目录
- python使用fpdf生成结账发票pdf文件
python使用fpdf生成结账发票pdf文件 目录 python使用fpdf生成结账发票pdf文件 #仿真数据 #生成pdf pip install fpdf
- python使用fpdf生成数据报告pdf文件
python使用fpdf生成数据报告pdf文件 目录 python使用fpdf生成数据报告pdf文件 # 报告生成整体代码
- 用python汇总pdf文件_Python处理PDF文件-简译与总结
最近看到一篇介绍Python中pyPDF模块的文章,详细介绍了使用pyPDF模块获取PDF文件信息,合并拆分PDF文件等功能.很方便,在此搬运分享以下: 全文介绍了以下几方面的功能 提取文件信息 旋转 ...
- Python应用——自定义函数:分割PDF文件函数
案例 将一个 pdf 文件按要求分割为几个部分.比如说一个pdf有20页,分成5个pdf文件,每个pdf文件包含4页.设计函数实现? Python代码 from PyPDF2 import PdfFi ...
- python基础教程第四版-python基础教程第4版pdf
python基础教程第4版pdf内容摘要 第二.针对青少年人群的计算机python视频教程.青少年的特点就是有个性.复杂多变.没有耐心.如果他们浏览一个计算机的时候,打开的比较慢,或者是计算机的课程凌 ...
- python实现pdf转word详解_手把手|20行Python代码教你批量将PDF文件转为Word格式(包教包会)...
在日常工作或学习中,经常会遇到这样的无奈: "小任,你把这个PDF中的文件码出来发我" 倒霉,2M的PDF12点也完不了啊! 很多时候在学习时发现许多文档都是PDF格式,PDF格式 ...
- python处理pdf 层_Python处理PDF及生成多层PDF实例代码
Python提供了众多的PDF支持库,本文是在Python3环境下,试用了两个库来完成PDF的生成的功能.PyPDF对于读取PDF支持较好,但是没找到生成多层PDF的方法.Reportlab看起来更成 ...
- Python办公自动化(三)|批量合并PDF
分享一个实用的办公脚本:将多个PDF合并为一个PDF,例如我手上现在有如下3个PDF分册,需要整合成一个完整的PDF 如果换成你操作的话,是不是打开百度搜索:PDF合并,然后去第三方网站操作,可能会收 ...
最新文章
- 离群点检测算法-基础概念
- AI人才抢夺“生猛”: 应届博士年薪涨到80万元
- 数据中心网络架构 — 云数据中心网络 — 二层架构设计示例
- linux 用户权限详解,03_04_Linux用户及权限详解
- WP8.1学习系列(第九章)——透视Pivot开发指南
- kvm虚拟化_KVM 虚拟化环境搭建 - WebVirtMgr
- SpringBoot 自动装配原理
- asp.net core2.0中网站发布的时候,怎么样才配置才可以使视图文件不被打包进去?...
- 华硕主板怎么开启tpm2.0
- 联想服务器改win7系统教程,联想笔记本Win10改Win7方法分享
- 厦门市建筑物矢量数据(Shp格式+带高度)
- Android屏幕、坐标系、Padding、Margin
- oppo enco free2 固件降级工具 (仅供测试使用)
- SpringSecurity实战(七)-对接第三方登陆-流程分析
- linux解压img镜像文件,Linux系统上解压或挂载img文件的方法
- C语言编程————杨辉三角
- matlab中set position,Matlab中set函数
- linux限制ssh 无公网ip白名单限制 基于网段
- Java模拟微信抢红包
- 不允许上传asp cer cdx htr文件时解决方法