Python win32com将word对象插入Excel文件

时间:2018-06-13 02:31:05

标签: python python-3.x win32com

Excel错误:尝试将多个对象插入Excel文件时“文档未保存”。适用于zip和.mpp文件扩展名,但不适用于.doc和.docx文件类型。插入后,对象显示一个白色的未知文件图像,只需说明EMBED(“文档”,“”)。但是,当我手动插入这些文档时,我没有任何问题,它们显示为Word doc图标。有什么建议吗?

import win32com.client as win32
xl = win32.gencache.EnsureDispatch('Excel.Application')
xl.Visible = 1
wb = xl.Workbooks.Open('BlankTemplate.xlsx')  #New Excel file that is blank
xl.DisplayAlerts = False
ws = wb.Worksheets('Sheet1')
dest_cell = ws.Range('C5')
obj = ws.OLEObjects()

obj.Add(ClassType = None,Filename ='Approval.zip',Link = False,DisplayAsIcon = False,Left = dest_cell.Left,Top = dest_cell.Top,Width = 50,Height = 50) obj.Add(ClassType = None,Filename ='Final.docx',Link = False,DisplayAsIcon = True,Left = dest_cell.Left,Top = dest_cell.Top,Width = 50,Height = 50) obj.Add(ClassType = None,Filename ='Release.doc',Link = False,DisplayAsIcon = True,Left = dest_cell.Left,Top = dest_cell.Top,Width = 50,Height = 50) obj.Add(ClassType = None,Filename ='Platform.mpp',Link = False,DisplayAsIcon = False,Left = dest_cell.Left,Top = dest_cell.Top,Width = 50,Height = 50)

wb.SaveAs('ProjectDetailsAttachments.xlsx')
xl.DisplayAlerts = True
xl.Application.Quit()
xl.Visible = 0    

1 个答案:

答案 0 :(得分:0)

我想通了!!我记录了一个Excel VBA宏,以便查看它用于插入word文档对象的内容,并发现它们添加了一些额外的参数,一旦我添加它们就可以了。

wordpath = 'C\\Program Files (x86)\\Microsoft Office\\Office 14\\WINWORD.EXE'
obj.Add(ClassType=None,Filename='Final.docx',Link=False,DisplayAsIcon=True, \ 
    IconFileName=wordpath,IconIndex=0,IconLabel=Final.docx \ 
    Left=dest_cell.Left, Top=dest_cell.Top,Width=50,Height=50)