admin管理员组文章数量:1593159
1.前言
有些时候,我们需要将pdf文档转换为word文档进行处理,但市面上的一些pdf软件往往需要付费才能使用。那么作为一名技术人员,如何才能实现pdf转word自由?
2.准备工作
提前安装好python的环境,并且安装对应的第三方包:
pip install pdf2docx
3.实现方法
3.1 convert方法
from pdf2docx import Converter
#需要转的pdf文件
pdf_file_path=r'test.pdf'
#输出的word文档
out_file_path=r'test.docx'
#进行转换
new_converter=Converter(pdf_file_path)
new_converter.convert(out_file_path)
new_converter.close()
3.2 parse方法
from pdf2docx import parse
#需要转的pdf文件
pdf_file_path=r'test.pdf'
#输出的word文档
out_file_path=r'test.docx'
#对所有页面进行转换,并输出
parse(pdf_file_path, out_file_path)
3.3 仅转换其中几页
from pdf2docx import Converter
#需要转的pdf文件
pdf_file_path=r'test.pdf'
#输出的word文档
out_file_path=r'test.docx'
#进行转换
new_converter=Converter(pdf_file_path)
#从第四页转换到最后一页,转换后输出
new_converter.convert(out_file_path,start=3)
#从第一页转换到第六页,转换后输出
new_converter.convert(out_file_path,end=6)
#从第四页转换到第六页,转换后输出
new_converter.convert(out_file_path,start=3,end=6)
new_converter.close()
转换其中的某些页面
#对pdf文档中的第1页、第四页和第九页就行转换,然后输出
new_converter.convert(out_file_path,pages=[0,3,8])
3.4 调用多进程
from pdf2docx import Converter
#需要转的pdf文件
pdf_file_path=r'test.pdf'
#输出的word文档
out_file_path=r'test.docx'
#进行转换
new_converter=Converter(pdf_file_path)
#进行多进程转换
new_converter.convert(out_file_path, multi_processing=True)
#指定数量
new_converter.convert(out_file_path, multi_processing=True, cpu_count=4)
new_converter.close()
3.5 处理有密码的pdf
from pdf2docx import Converter
#需要转的pdf文件
pdf_file_path=r'test.pdf'
#输出的word文档
out_file_path=r'test.docx'
#加密的pdf文档密码
pwd='pdf_2_word'
#进行转换
new_converter=Converter(pdf_file_path)
new_converter.convert(out_file_path,pwd)
new_converter.close()
3.6 将pdf转为表格
注意,本方法只能将以表格形式生成的pdf文档进行转换,不能处理含有表格的图片形式pdf文档。
from pdf2docx import Converter
import pandas as pd
pdf_file_path=r'test.pdf'
out_file_path=r'test.xlsx'
cv = Converter(pdf_file_path)
tables = cv.extract_tables()
cv.close()
# for table in tables:
# print(table)
df=pd.DataFrame(tables[1:],columns=tables[0])
df.to_excel(out_file_path,encoding='utf-8-sig')
print(df)
4.后记
以上就是pdf转为word的方法,可以根据个人的情况进行调整。希望能够帮助到你~
版权声明:本文标题:如何利用python将pdf文档转为word? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dongtai/1728165869a1148009.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论