0

I am trying to use python to read each sheets in excelworkbook and to write to existing word document.

Code snip as below:

from win32com import client
excel = client.Dispatch("Excel.Application")
word = client.Dispatch("Word.Application")
doc = word.Documents.Open("D:/xx.docx")
xl = excel.Workbooks.Open("D:/yy.xlsx")


for i in xl.sheet_names:

    xl_sheet = xl_workbook.sheet_by_name(sheet_names[i])
    xl.Range("A1:D20").Copy()      

However, encounter error:

for i in xl.sheet_names:
  Local\Programs\Python\Python38-32\lib\site-packages\win32com\client\dynamic.py", line 527, in __getattr__
    raise AttributeError("%s.%s" % (self._username_, attr))
AttributeError: Open.sheet_names

Kindly help me in this solving this issue

2

1 Answer 1

0

You can try the following.

from win32com import client

# If you want to use client.constants, you should use client.gencache.EnsureDispatch instead of client.Dispatch
word = client.gencache.EnsureDispatch("Word.Application")
excel = client.gencache.EnsureDispatch("Excel.Application")
doc = word.Documents.Open("D:/xx.docx")
wb = excel.Workbooks.Open("D:/yy.xlsx")

# If you set Visible = True, it is easier to check if things are going right.
word.Visible = True
excel.Visible = True

# Doc for Word.Document interface: https://learn.microsoft.com/en-us/dotnet/api/microsoft.office.interop.word.document
range = doc.Content  # Or, range = doc.Range()

# Doc for Excel.Workbook Interface: https://learn.microsoft.com/en-us/dotnet/api/microsoft.office.interop.excel.workbook
# Sheets property here: https://learn.microsoft.com/en-us/dotnet/api/microsoft.office.interop.excel.workbook#microsoft-office-interop-excel--workbook-sheets
for ws in wb.Sheets:
    # Doc for Word.Range.Collapse method: https://learn.microsoft.com/en-us/dotnet/api/microsoft.office.interop.word.range.collapse
    range.Collapse(client.constants.wdCollapseEnd)
    ws.Range("A1:D20").Copy()
    range.Paste()  # There is also PasteExcelTable method. Check the doc: https://learn.microsoft.com/en-us/dotnet/api/microsoft.office.interop.word.range.pasteexceltable

You can refer the docs on the following links.

Word: https://learn.microsoft.com/en-us/dotnet/api/microsoft.office.interop.word

Excel: https://learn.microsoft.com/en-us/dotnet/api/microsoft.office.interop.excel

2
  • Thanks. it solve the problem. Could the format that in the Excel be copy as what it is to Word? The format change in word after copied from Excel using above python code. May 13, 2020 at 2:01
  • @user13487681 You mean the table format? Then, you can use Range.PasteExcelTable method which is mentioned in the comment on the right side of range.Paste(). You can try range.PasteExcelTable(False, False, True) instead of range.Paste().
    – Gorisanson
    May 13, 2020 at 8:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.