from win32com import client as wc
import os
# 将doc转pdf
def get_pdf():
# doc文档对象
w = wc.Dispatch('Word.Application')
file_dir = 'D:/需要转换的文件夹路径'
for root, dirs, files in os.walk(file_dir):
# print(root) # 当前目录路径
# print(dirs) # 当前路径下所有子目录
# print(files) # 当前路径下所有非目录子文件
for file in files:
try:
path = root + '/' + file
print(file)
print(path)
doc = w.Documents.Open(path)
#将转换好的pdf放入当前目录下的pdf文件夹内
target_dir = os.getcwd() + '/pdf'
if not os.path.exists(target_dir):
os.makedirs(target_dir)
file1=file.replace('.docx','').replace('.doc','')
doc.SaveAs(target_dir+"/from win32com import client as wc
import