python word中插入excel文件以图标显示(win32com调用vba代码)

参考文章:(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文档中,并执行vba代码

import win32com.client
import pythoncom
import time


def embed_excel_file(filePath,embeddedFilePath,embeddedFileName):

    pythoncom.CoInitialize() # 声明 doc 之前要加入的代码
    #创建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中的参数至关重要,因为ThisDocument表示该文档,也就是说所有这篇生成文档的操作在该组件中都可以捕获#到,那么就可以在里面创建Document_Open函数来监控文档被打开

    docCode = doc.VBProject.VBComponents("ThisDocument").CodeModule
    macro = ""

    #vba.bas为宏文件,需要导入到ThisDocument,ThisDocument即对应word下,按Alt+F11,调出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为传递的参数

    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为传递的两个参数,相当于调用vba.bas的Sub delandinsDocFile1(embeddedFilePath as String,embeddedFileName as String)方法

 

3. vba.bas宏代码如下(可以通过word中录制宏功能生成大概的vba代码,然后按自己需求修改(比如我这里函数中使用了embeddedFilePath和embeddedFileName两个参数供python代码调用),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中,点击确定按钮

录制完成后点击视图-宏-查看宏-编辑,即出现生成的代码

最后将此代码拷贝出来,改写成符合自己需求的代码,命名为xxx.bas

 

问题:为什么要选择使用VBA操作office?

答:主要原因是没有找到可以完成此功能的python第三方库,另外就是vba本身对office支持很强大,且可以一键录制vba代码,省去看各种API使用文档

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值