參考文章:(5條消息) python extension(pywin32) 插入宏到word_hit_liuanhuaming的專欄-CSDN博客
?
功能需求:在C:\Users\user\Desktop\20210408-1\xxx.docx中插入xxx.xlsx文件以圖標顯示,如下圖:
?
1.準備:
1)python模塊安裝:
pip install pypiwin32
2)word啟用宏功能:
文件-選項
信任中心-信任中心設置
勾選信任對VBA工程對象模型的訪問
2. python代碼:
目標:將C:\\Users\\user\\Desktop\\tianqing-auto\\vba.bas(vba)代碼自動嵌入xxx.doc文檔中,并執(zhí)行vba代碼
import win32com.client
import pythoncom
import time
def embed_excel_file(filePath,embeddedFilePath,embeddedFileName):
pythoncom.CoInitialize() # 聲明 doc 之前要加入的代碼
#創(chuàng)建word應用程序實例
docApp = win32com.client.Dispatch('Word.Application')
# run in back-control and no warnings
doc=docApp.Documents.Open(filePath)
docApp.Visible = 1 #值為0操作文檔不可見,為1可見
docApp.DisplayAlerts = 0 #不顯示警告信息
#導入宏代碼
#通過文檔實例,獲取VBProject的組件,其中VBComponents中的參數(shù)至關重要,因為ThisDocument表示該文檔,也就是說所有這篇生成文檔的操作在該組件中都可以捕獲#到,那么就可以在里面創(chuàng)建Document_Open函數(shù)來監(jiān)控文檔被打開
docCode = doc.VBProject.VBComponents("ThisDocument").CodeModule
macro = ""
#vba.bas為宏文件,需要導入到ThisDocument,ThisDocument即對應word下,按Alt+F11,調(diào)出vba窗口,該文檔下的Microsoft Word對象下的ThisDocument。
string=open("C:\\Users\\user\\Desktop\\tianqing-auto\\vba.bas")
macro=string.read()
docCode.AddFromString(macro)
print("embeddedFileName:",embeddedFileName)
docApp.Application.Run("delandinsDocFile1",embeddedFilePath,embeddedFileName)
#運行vba.bas中的delandinsDocFile1方法,embeddedFilePath和embeddedFileName為傳遞的參數(shù)
doc.Save()
doc.Save()
time.sleep(3)
doc.Close()
docApp.Quit()
pythoncom.CoUninitialize() # 關閉 doc 之后加入的代碼
filePath=r'C:\Users\user\Desktop\20210408-1\xxx.docx'
dest_dir1=r'C:\Users\user\Desktop\20210408-1\\'
embeddedFileName='xxx.xlsx'
embeddedFilePath=dest_dir1 + embeddedFileName
embed_excel_file(filePath,embeddedFilePath,embeddedFileName)
?
注:? ? Python代碼中的docApp.Application.Run("delandinsDocFile1",embeddedFilePath,embeddedFileName)代表運行vba.bas中的delandinsDocFile1方法,而embeddedFilePath和embeddedFileName為傳遞的兩個參數(shù),相當于調(diào)用vba.bas的Sub delandinsDocFile1(embeddedFilePath as String,embeddedFileName as String)方法
?
3. vba.bas宏代碼如下(可以通過word中錄制宏功能生成大概的vba代碼,然后按自己需求修改(比如我這里函數(shù)中使用了embeddedFilePath和embeddedFileName兩個參數(shù)供python代碼調(diào)用),word錄制宏功能稍后在文章尾部提供
Sub delandinsDocFile1(embeddedFilePath as String,embeddedFileName as String)
'
' delandinsDocFile1 宏
'
'
Selection.EndKey Unit:=wdStory
Selection.MoveUp Unit:=wdLine, Count:=6
Selection.TypeBackspace
'定位插入位置
'
Selection.Delete Unit:=wdCharacter, Count:=1
'刪除之前插入的文件,下面插入excel文件
'
Selection.InlineShapes.AddOLEObject ClassType:="Excel.Sheet.12", FileName _
:=embeddedFilePath _
, LinkToFile:=False, DisplayAsIcon:=True, IconFileName:= _
"C:\Windows\Installer\{90140000-0011-0000-0000-0000000FF1CE}\xlicons.exe" _
, IconIndex:=1, IconLabel:=embeddedFileName
ActiveDocument.Save
End Sub
?
4. word錄制宏功能:
?
填入宏名稱,將宏保存在打開的word中,點擊確定按鈕
錄制完成后點擊視圖-宏-查看宏-編輯,即出現(xiàn)生成的代碼
最后將此代碼拷貝出來,改寫成符合自己需求的代碼,命名為xxx.bas
?
問題:為什么要選擇使用VBA操作office?文章來源:http://www.zghlxwxcb.cn/news/detail-404049.html
答:主要原因是沒有找到可以完成此功能的python第三方庫,另外就是vba本身對office支持很強大,且可以一鍵錄制vba代碼,省去看各種API使用文檔文章來源地址http://www.zghlxwxcb.cn/news/detail-404049.html
到了這里,關于python word中插入excel文件以圖標顯示(win32com調(diào)用vba代碼)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!