import os,time from docx import Document from docxcompose.composer import Composer import win32com.client as win32 # 獲取要處理的文件夾路徑 folder_path = r"C:\Users\Thinkpad\Desktop\wordoutput" datanames = os.listdir(folder_path) list_wordoutname = [] for i in datanames: # 獲取文件名稱中日期 list_wordoutname.append(folder_path+"\\"+i) print(list_wordoutname) #以下函數(shù)用于合并指定文件夾中所有word文件 # 創(chuàng)建新的空白Word文檔 merged_doc = Document() def HB_wordwj(files, final_docx): new_document = Document() composer = Composer(new_document) for fn in files: composer.append(Document(fn)) composer.save(final_docx) # 保存合并后的文檔 desktop_path = os.path.expanduser("~\Desktop") #獲取當前桌面路徑 merged_file_name = desktop_path+"\\"+"合并輸出文件.docx" print(merged_file_name) HB_wordwj(list_wordoutname,merged_file_name) print("已將文件夾中的所有Word文件合并為", merged_file_name) #------以下程序用于執(zhí)行word中宏程序,用于調(diào)整合并插入的圖片大小--------------------- # 創(chuàng)建 Word 應用程序?qū)ο?word = win32.gencache.EnsureDispatch('Word.Application') # 打開文件 doc = word.Documents.Open(merged_file_name) try: # 運行宏 doc.Application.Run("setpicsize") finally: # 關閉文件并退出 Word 應用程序 doc.Close() word.Quit()
注:setpicsize宏文件先要放如word中的視圖-宏中,宏文件代碼如下
Sub setpicsize()
Dim n
On Error Resume Next文章來源:http://www.zghlxwxcb.cn/news/detail-785879.html
For n = 1 To ActiveDocument.InlineShapes.Count
ActiveDocument.InlineShapes(n).Height = 27.31 * 20
ActiveDocument.InlineShapes(n).Width = 19.33 * 20
Next n
End Sub
?文章來源地址http://www.zghlxwxcb.cn/news/detail-785879.html
到了這里,關于用python批量合并word文件并統(tǒng)一調(diào)整圖片大小的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!