admin管理员组

文章数量:1605131

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+'转换成功')

本文标签: 代码批量PythonPDFword