Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
652 views
in Technique[技术] by (71.8m points)

win32com - python Copy excel file Char into word file failure.(Use Clipboard) (Word.Application)

I am searching for a long time on net. But no use. Please help or try to give some ideas how to achieve this

python code Exec failure: process[word.Selection.Paste] is Invalid.

from win32com import client

excel = client.Dispatch("Excel.Application")

word = client.Dispatch("Word.Application")


doc = word.Documents.Open("D:/python/My_Work/exe/word-opt/word_file.docx")

book = excel.Workbooks.Open("D:/python/My_Work/exe/word-opt/excel_file.xlsx")

char1=sheet.ChartObjects(1).Copy

doc.Bookmarks("BookMark2").Range.Select

word.Selection.Paste

doc.Save()

doc.Close()

the Same VBA code Exec OK:

Sub test()

Set dataExcel = CreateObject("Excel.Application")

Set Workbook = dataExcel.Workbooks.Open(ThisWorkbook.Path & "excel_file.xlsx")

Set Sheet = Workbook.Worksheets(1)

Dim WordApp As Word.Application 

Dim WordDoc As Word.Document 

Set WordApp = New Word.Application

WordApp.Visible = True

Set WordDoc = WordApp.Documents.Open(ThisWorkbook.Path & "word_file.docx") 

Sheet.ChartObjects(1).Copy

WordDoc.Bookmarks("BookMark2").Range.Select

WordApp.Selection.Paste

WordDoc.Save 

WordDoc.Close

WordApp.Quit 

Set WordDoc = Nothing

Set WordApp = Nothing

End Sub

ex: File [excel_file.xlsx] in Only a chart.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...