admin管理员组文章数量:1605132
1.单个pdf转word
pdf2docx的下载:pip install pdf2docx -i https://pypi.tuna.tsinghua.edu/simple
from pdf2docx import Converter
#pdf转word
a = Converter(r'D:\Pycharm\Company\work_test\2.pdf') #pdf的路径
#输出的word的路径加文件名,不用自己创建,会自动创建,可将pdf路径直接复制下来,将后缀pdf改为docx就可以了。
a.convert(r'D:\Pycharm\Company\work_test\2.docx')
a.close() #一定要有,释放内存用的
2.多个pdf转word
from pdf2docx import Converter
import os
#批量pdf转word
path = 'D:/Pycharm/Company/work_test/'
# 定义空list,存放文件夹中的文件名
files = []
for file in os.listdir(path):
if file.endswith(".pdf"):
files.append(path+file)
for file in files:
a = Converter(file)
a.convert(file.split('.')[0]+'.docx') #创建生成的docx文件和文件名的文件名
a.close()
print(file+'转换成功')
3.单个word转pdf
docx2pdf 的下载:pip install docx2pdf -i https://pypi.tuna.tsinghua.edu/simple
from docx2pdf import convert
# 第一个为word路径,第二个为生成的pdf路径,不用自己创建,会自动创建,可将docx路径直接复制下来,将后缀docx改为pdf就可以了
convert("D:/Pycharm/Company/work_test/output.docx", "D:/Pycharm/Company/work_test/output.pdf")
4.多个word转多个pdf
from docx2pdf import convert
import os
# 文件位置
path = 'D:/Pycharm/Company/work_test/'
# 定义空list,存放文件夹中的文件名
files = []
for file in os.listdir(path):
if file.endswith(".docx"):
files.append(path+file)
for file in files:
convert(file,file.split('.')[0]+'.pdf') #创建生成的pdf文件和文件名的文件名
print(file+'转换成功')
版权声明:本文标题:python实现pdf转word(4行代码),word转pdf(2行代码)。(单个和批量) 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dongtai/1728478860a1159905.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论