Hello I am new users of Jupyter python and I have a question:
I have data in xls sheet and I want to write some of the columns in words docx by using python.
I have 12 columns and I want only 3 in docx file.
Advertisement
Answer
In Windows, name the range of the 3 columns you’re trying to copy in Excel. You can then paste it in your Word document using the following bit:
from win32com import client excel = client.Dispatch("Excel.Application") word = client.Dispatch("Word.Application") doc = word.Documents.Open("C:/word_file.docx") book = excel.Workbooks.Open("C:/excel_file.xlsx") sheet = book.Worksheets(1) #depends on which sheet your named range is located sheet.Range(NAMED_RANGE_OF_YOUR_TABLE_IN_EXCEL).Copy() target = doc.Range() findtext = "WHERE_I_WANT_TO_PASTE_MY_TABLE_IN_WORD" #use a placeholder in your word document where you want the table to appear if target.Find.Execute(FindText=findtext): table_range = doc.Range(Start = target.start, End=target.end) table_range.PasteExcelTable(False, False, False)
It will keep the formatting from the workbook.