使用Python将Microsoft Word文档转换为PDF

时间:2018-10-31 06:02:54

标签: python python-3.x pdf ms-word

我有大量的Word和Excel文件。我想将子文件夹中的许多Word文件转换为PDF,然后尝试执行以下代码。

此代码未激活(我没有将Word转换为PDF),尽管没有错误。

Enter image description here

可能是什么问题?还有其他解决方案吗?

这是我的代码:

import os
from win32com import client
path = 'D:\programing\test'
word_file_names = []
word = client.DispatchEx("Word.Application")
for dirpath, dirnames, filenames in os.walk(path):
    print (dirpath)
    for f in filenames:
        if f.lower().endswith(".docx") and re.search('Addendum', f):
            new_name = f.replace(".docx", r".pdf")
            in_file = word_file_names.append(dirpath + "\\" + f)
            new_file = word_file_names.append(dirpath + "\\" + new_name)
            doc = word.Documents.Open(in_file)
            doc.SaveAs(new_file, FileFormat = 17)
            doc.Close()
        if f.lower().endswith(".doc") and re.search('Addendum', f):
            new_name = f.replace(".doc", r".pdf")
            in_file = word_file_names.append(dirpath + "\\" + f)
            new_file = word_file_names.append(dirpath + "\\" + new_name)
            doc = word.Documents.Open(in_file)
            doc.SaveAs(new_file, FileFormat = 17)
            doc.Close()
    word.Quit()

3 个答案:

答案 0 :(得分:0)

我解决了这个问题,并修复了以下代码

import os
import win32com.client
import re
path = (r'D:\programing\test')
word_file_names = []
word = win32com.client.Dispatch('Word.Application')
for dirpath, dirnames, filenames in os.walk(path):
    for f in filenames:  
        if f.lower().endswith(".docx") :
            new_name = f.replace(".docx", ".pdf")
            in_file =(dirpath + '/'+ f)
            new_file =(dirpath + '/' + new_name)
            doc = word.Documents.Open(in_file)
            doc.SaveAs(new_file, FileFormat = 17)
            doc.Close()
        if f.lower().endswith(".doc"):
            new_name = f.replace(".doc", ".pdf")
            in_file =(dirpath +'/' + f)
            new_file =(dirpath +'/' + new_name)
            doc = word.Documents.Open(in_file)
            doc.SaveAs(new_file, FileFormat = 17)
            doc.Close()
word.Quit()

答案 1 :(得分:0)

您可以使用comtypes

from comtypes.client import CreateObject
import os

folder = "folder path"
wdToPDF = CreateObject("Word.Application")
wdFormatPDF = 17
files = os.listdir(folder)
word_files = [f for f in files if f.endswith((".doc", ".docx"))]
for word_file in word_files:
    word_path = os.path.join(folder, word_file)
    pdf_path = word_path
    if pdf_path[-3:] != 'pdf':
        pdf_path = pdf_path + ".pdf"

    if os.path.exists(pdf_path):
        os.remove(pdf_path)

    pdfCreate = wdToPDF.Documents.Open(word_path)
    pdfCreate.SaveAs(pdf_path, wdFormatPDF)

答案 2 :(得分:0)

这更容易:

from docx2pdf import convert

convert(word_path, pdf_path)