Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Python将Microsoft Word文档转换为PDF_Python_Python 3.x_Pdf_Ms Word - Fatal编程技术网

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

使用Python将Microsoft Word文档转换为PDF,python,python-3.x,pdf,ms-word,Python,Python 3.x,Pdf,Ms Word,我有很多Word和Excel文件。我想将文件夹中的许多Word文件按子文件夹转换为PDF,并尝试以下代码 虽然没有错误,但此代码不是活动的(我的意思是没有Word转换为PDF) 有什么问题吗?还有别的解决办法吗 这是我的代码: import os from win32com import client path = 'D:\programing\test' word_file_names = [] word = client.DispatchEx("Word.Application") for

我有很多Word和Excel文件。我想将文件夹中的许多Word文件按子文件夹转换为PDF,并尝试以下代码

虽然没有错误,但此代码不是活动的(我的意思是没有Word转换为PDF)

有什么问题吗?还有别的解决办法吗

这是我的代码:

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()

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

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()
你可以使用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)
这更简单:

from docx2pdf import convert

convert(word_path, pdf_path)

这个问题以前已经回答过,这可能会帮助您:是否有任何输出?你能展示一下正在打印的东西吗?你试过在调试器中运行这个吗?我已经在我的Tobic中添加了结果照片。你已经注释掉了
除了
块,你确定代码没有抛出任何错误吗?好的,ankit,我总结了我的代码只是为了重要的事情(再次查看),如果你能提供帮助,这需要安装microsoft word才能工作。否则,这正是我想要的。