簡體   English   中英

Python Win32com將Word對象插入Excel文件

[英]Python win32com insert word object into Excel file

Excel錯誤:嘗試將多個對象插入excel文件時出現“未保存文檔”。 適用於zip和.mpp文件擴展名,但不適用於.doc和.docx文件類型。 插入后,該對象將顯示白色的未知文件圖像,並簡單地說EMBED(“ Document”,“”)。 但是,當我手動插入這些文檔時,我沒有任何問題,它們顯示為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 =無,文件名='Final.docx',鏈接= False,DisplayAsIcon = True,左= dest_cell.Left,頂部= dest_cell.Top,寬度= 50,高度= 50)obj.Add(ClassType = None,文件名='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,左= dest_cell.Left,頂= dest_cell.Top,寬度= 50,高度= 50)

wb.SaveAs('ProjectDetailsAttachments.xlsx')
xl.DisplayAlerts = True
xl.Application.Quit()
xl.Visible = 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) 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM