通过pywin32将pandas数据框写入Word文档表 [英] Writing a pandas dataframe to a word document table via pywin32
问题描述
我目前正在处理一个脚本,出于演示目的,该脚本需要写入.docx文件.我使用熊猫来处理脚本中的所有数据计算.我希望使用PyWIN32将pandas数据帧写入word.docx文件中书签的表中.数据框由浮点数组成.伪代码是这样的.
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'])
已导入pywin32 ...
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
现在,我想在此书签中创建一个表.表格的尺寸应由数据框决定.我也希望列标题成为Word表中的标题.
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
# data frame
df= DataFrame(np.arange(28).reshape((4,7)), columns=['Text1',...'Text7'])
wordApp = win32.gencache.EnsureDispatch('Word.Application')
wordApp.Visible = False
doc = wordApp.Documents.Open(os.getcwd()+'\\template.docx')
rng = doc.Bookmarks("PUTTABLEHERE").Range
# creating Table
# add one more row in table at word because you want to add column names as header
Table=rng.Tables.Add(rng,NumRows=df.shape[0]+1,NumColumns=df.shape[1])
for col in range(df.shape[1]):
# Writing column names
Table.Cell(1,col+1).Range.Text=str(df.columns[col])
for row in range(df.shape[0]):
# writing each value of data frame
Table.Cell(row+1+1,col+1).Range.Text=str(df.iloc[row,col])
请注意,这里在Table.Cell(row+1+1,col+1)
中添加了两个.原因是因为Microsoft Word中的表从1开始索引.因此,行和col都必须添加1,因为熊猫的数据帧索引从0开始.
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.
在行的末尾添加另一个1,以为数据帧列留出空间作为标题.那应该做!
Another 1 is added at row to give space for data frame columns as headers. That should do it !
这篇关于通过pywin32将pandas数据框写入Word文档表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!