Write tables from Word (.docx) to Excel (.xlsx) using xlsxwriter

BBT aishan0403 at gmail.com
Fri May 29 02:00:13 EDT 2020


On Thursday, 28 May 2020 03:07:48 UTC+8, Peter Otten  wrote:
> BBT wrote:
> 
> > I tried your code by replacing the Document portion:
> 
> > But I received an error:
> > TypeError: __init__() takes 1 positional argument but 2 were given
> 
> We seem to have different ideas of what replacing means. 
> Here is the suggested script spelt out:
> 
> import xlsxwriter
>  
> from docx.api import Document
> document = Document('/Users/xxx/Documents/xxx/Clauses Sample - Copy v1 - for merge.docx')
> 
> wb = xlsxwriter.Workbook('C:/Users/xxx/Documents/xx/test clause retrieval.xlsx')
> sheet = wb.add_worksheet("Compliance")
>  
> offset = 0
> for table in document.tables:
>     for y, row in enumerate(table.rows):
>         for x, cell in enumerate(row.cells):
>             sheet.write(y + offset, x, cell.text)
>     offset +=  len(table.rows) + 1  # one empty row between tables
> 
> wb.close()

Yes! That works well, thank you Peter! And I'm impressed by your object-oriented simulation too. Would look into it again :)


More information about the Python-list mailing list