I am currently working on a script that needs to write to a .docx file for presentation purposes. I use pandas to handle all my data calculations in the script. I am looking to write a pandas dataframe into a table at a bookmark in a word.docx file using PyWIN32. The dataframe consists of floats. The psuedo code is something like this.
frame = DataFrame(np.arange(28).reshape((4,7)), columns=['Text1',...'Text7'])
With pywin32 imported...
wordApp = win32.gencache.EnsureDispatch('Word.Application')
wordApp.Visible = False
doc = wordApp.Documents.Open(os.getcwd()+'\\template.docx')
rng = doc.Bookmarks("PUTTABLEHERE").Range
rng.InsertTable.here
Now i would like to create a table at this bookmark. The dimensions of the table should be dictated by the dataframe. I would also like the column titles to be the header in the Word table.
Basically, all you need to do is create a table in word and populate the values of each cell from the corresponding values of data frame
Notice that
Table.Cell(row+1+1,col+1)
has been added two ones here. The reason is because Table in Microsoft Word start indexing from 1. So, both row and col has to be added 1 because data frame indexing in pandas start from 0.Another 1 is added at row to give space for data frame columns as headers. That should do it !