使用Python3.6批量将文件夹中的所有docx文档转为PDF

  老大让我把资料发给客户,发之前先转为PDF,资料储存路径大概是这样子:

然后发挥主观能动性,打算用Python来完成,百度了一下,网络上的并不能很好满足我的需求,然后就自己敲了一段以供大家参考。 

# -*- coding: utf-8 -*-
"""
Created on Thu Feb 14 09:37:40 2019

@author: 45543
"""

import os
from win32com import client as wc

rootdir = r'G:\验指导书0814'  # 文件夹路径
save_path = r'C:\Users\45543\Desktop\tmp'   # PDF储存位置

word = wc.Dispatch('Word.Application')  # 首先将doc转换成docx
del_file = []
try:
    f_list = []
    os_dict = {root:[dirs, files] for root, dirs, files in os.walk(rootdir)}
    for parent, dirnames, filenames in os.walk(rootdir):
        for filename in filenames:
            if u'.docx' in filename and u'~$' not in filename:
                title = filename[:-5]  # 删除.docx
#                print(title)
                f_list.append(filename)
                word.Visible = 0
                doc = word.Documents.Open(os.path.join(parent, filename))
                doc.SaveAs(os.path.join(save_path, title+'.pdf') ,17)  # 直接保存为PDF文件
                doc.Close()
finally:
    word.Quit()
print(f_list)  # 输出已转换的文件名称

 

你可能感兴趣的:(Python)