||
参考link
小型表格
import docx import pandas as pd data = pd.read_excel("some_data.xlsx") doc = docx.Document() table = doc.add_table(rows=data.shape[0], cols=data.shape[1]) for i in range(df.shape[0]): for j in range(df.shape[1]): table.cell(i,j).text = str(df.values[i,j]) doc.save("output_file_path.docx")
大型表格,使用 table_cells = table._cells将所有单元格抽取出来,
在对抽取出来的单元格单独处理,避免对表格循环
import docx import pandas as pd data = pd.read_excel("some_bigger_data.xlsx") doc = docx.Document() table = doc.add_table(rows=data.shape[0], cols=data.shape[1]) table_cells = table._cells for i in range(data.shape[0]): for j in range(data.shape[1]): table_cells[j + i * data.shape[1]].text = str(data.values[i][j]) doc.save("output_file_path.docx")
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-12-14 07:34
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社