在絕對的力量面前,任何技巧都是徒勞,但在數量面前...
先看效果
運行代碼後,會自動打開每個word文件(支持doc和docx),然後生成對應的pdf文件。
再看代碼
直接改一下doc_path(存放word文件的路徑)和save_path(生成pdf文件的路徑)即可運行。
<code>import os
import win32com.client
class Word2PDF(object):
def __init__(self, filepath, Debug=True):
self.wordApp = win32com.client.Dispatch('word.Application')
self.wordApp.Visible = Debug
self.myDoc = self.wordApp.Documents.Open(filepath)
def export_pdf(self, output_file_path):
self.myDoc.ExportAsFixedFormat(output_file_path, 17, Item=7, CreateBookmarks=0)
def close(self):
self.wordApp.Quit()
if __name__ == '__main__':
doc_path = 'D:/my_files'
save_path = 'D:/my_files'
for filename in os.listdir(doc_path):
if '.doc' in filename:
result = Word2PDF(os.path.join(doc_path, filename), True)
new_name = filename.split('.')[0] + '.pdf'
result.export_pdf(os.path.join(save_path, new_name))
print('ok')/<code>
關注微信公眾號“Python小鎮”,發現更多幹貨知識!包括源碼哦 ~
閱讀更多 Python小鎮 的文章