Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
603 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 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...