我有大量的 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()
原文由 Youssri Abo Elseod 发布,翻译遵循 CC BY-SA 4.0 许可协议
我解决了这个问题并修复了代码如下