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

Nandu Raj
  • 2,072
  • 9
  • 20
  • see [here](https://stackoverflow.com/questions/22756344/how-do-i-extract-data-from-a-doc-docx-file-using-python), [here](https://stackoverflow.com/questions/10366596/how-to-read-contents-of-an-table-in-ms-word-file-using-python/33775294#33775294) and try looking into pandas [here](https://stackoverflow.com/questions/29459461/dataframe-to-excel-sheet/29461151#29461151) and [here](https://stackoverflow.com/questions/47977367/how-to-create-a-dataframe-from-a-table-in-a-word-document-docx-file-using-pan) – impopularGuy May 07 '20 at 03:24
  • Does this answer your question? [How do I extract data from a doc/docx file using Python](https://stackoverflow.com/questions/22756344/how-do-i-extract-data-from-a-doc-docx-file-using-python) – impopularGuy May 07 '20 at 03:28

1 Answers1

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

Gorisanson
  • 2,202
  • 1
  • 9
  • 25
  • 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. – user13487681 May 13 '20 at 02: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 '20 at 08:36