Write tables from Word (.docx) to Excel (.xlsx) using xlsxwriter - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Write tables from Word (.docx) to Excel (.xlsx) using xlsxwriter (/thread-27144.html) |
Write tables from Word (.docx) to Excel (.xlsx) using xlsxwriter - jackie - May-27-2020 I am trying to parse a word (.docx) for tables, then copy these tables over to excel using xlsxwriter. This is my code: from docx.api import Document import xlsxwriter document = Document('/Users/xxx/Documents/xxx/Clauses Sample - Copy v1 - for merge.docx') tables = document.tables wb = xlsxwriter.Workbook('C:/Users/xxx/Documents/xxx/test clause retrieval.xlsx') Sheet1 = wb.add_worksheet("Compliance") index_row = 0 print(len(tables)) for table in document.tables: data = [] keys = None for i, row in enumerate(table.rows): text = (cell.text for cell in row.cells) if i == 0: keys = tuple(text) continue row_data = dict(zip(keys, text)) data.append(row_data) #print (data) #big_data.append(data) Sheet1.write(index_row,0, str(row_data)) index_row = index_row + 1 print(row_data) wb.close()This is my desired output: However, here is my actual output: I am aware that my current output produces a list of string instead. Is there anyway that I can get my desired output using xlsxwriter? RE: Write tables from Word (.docx) to Excel (.xlsx) using xlsxwriter - mcmxl22 - May-27-2020 Don't zip the keys and text into a dict. Put them into the columns and rows as shown here. |