用Python批量地将.doc和.docx两种word文件转换为pdf文件(Windows系统下验证可行)

单个word(.doc或.docx)文件到pdf文件的转换可以通过Word输出另存为功能来实现,但当文件个数很多时,重复性操作就变地很麻烦。于是我想到利用Python写一个批量转换的脚本,经测试,该脚本可对.doc及.docx文件中的一种或两种进行pdf转换。代码如下:

# convert .doc or .docx files to .pdf files in bulk
import os
from win32com.client import Dispatch

def word2pdf(abs_path,obj_path,files) : # word到pdf的转换
    if os.path.exists(obj_path) :
        print("目标文件夹已经存在")
    else :
        os.mkdir(obj_path)
        print("目标文件夹创建成功")
    file_count=0
    word=Dispatch("Word.Application")
    for file in files :
        file_count+=1
        doc=word.Documents.Open(abs_path+file)
        if file.endswith(".doc") :
            doc.SaveAs(obj_path+file.replace(".doc" ,".pdf"),FileFormat=17)
        else :
            doc.SaveAs(obj_path+file.replace(".docx" ,".pdf"),FileFormat=17)
        doc.Close()
    word.Quit()
    return file_count

def get_file(abs_path) : # 获取待转化的word文件名
    filenames=[]
    raw_names=os.listdir(abs_path)
    for raw_name in raw_names :
        if raw_name.endswith(".docx") or raw_name.endswith(".doc") :
            filenames.append(raw_name)
        else :
            continue
    return filenames

if __name__ == "__main__" :
    abs_path=("C:/Users/Young/Desktop/") # word文件存放地址
    obj_path=("C:/Users/Young/Desktop/pdf_folder/")# 转换后的pdf文件存放地址
    files=get_file(abs_path)
    file_number=word2pdf(abs_path,obj_path,files)
    print("{}个word文件已转换为相应pdf文件".format(file_number))
    

经检测,可以实现预期目的,输出如下:

目标文件夹创建成功
4个word文件已转换为相应pdf文件
>>> 

需要注意的是,首先需要在cmd命令窗口输入“pip install pywin32"导入win32库。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要将PDF文件转换DOCX文件,您可以使用Python的“pdfminer”和“python-docx”库。您需要安装这些库,然后按照以下步骤进行操作: 1. 导入所需的库: ```python import io import os import docx from pdfminer.converter import TextConverter from pdfminer.pdfinterp import PDFPageInterpreter from pdfminer.pdfinterp import PDFResourceManager from pdfminer.pdfpage import PDFPage ``` 2. 创建一个函数来将PDF文件转换为文本: ```python def pdf_to_text(pdf_file): resource_manager = PDFResourceManager() text_stream = io.StringIO() codec = 'utf-8' laparams = pdfminer.layout.LAParams() converter = TextConverter(resource_manager, text_stream, codec=codec, laparams=laparams) interpreter = PDFPageInterpreter(resource_manager, converter) password = "" maxpages = 0 caching = True page_nums = set() for page in PDFPage.get_pages(pdf_file, page_nums, maxpages=maxpages, password=password, caching=caching, check_extractable=True): interpreter.process_page(page) converter.close() text = text_stream.getvalue() text_stream.close() return text ``` 3. 创建一个函数来将文本转换DOCX文件: ```python def text_to_docx(text, output): doc = docx.Document() doc.add_paragraph(text) doc.save(output) ``` 4. 最后,您可以将上面的两个函数组合在一起来实现转换: ```python pdf_file = open('example.pdf', 'rb') text = pdf_to_text(pdf_file) pdf_file.close() output = 'example.docx' text_to_docx(text, output) ``` 以上代码中,我们将PDF文件“example.pdf转换为文本,然后将文本转换DOCX文件“example.docx”。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IC_Young

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值