我需要将数据帧数据写入Word文档中的现有表格。文档中的表格已经有两行,我需要在这两行之后添加 df 行。使用
python-docx
库。
document = Document(docx=document_name)
table = document.tables[0]
for i in range(orders.shape[0]):
table.add_row()
for i in range(orders.shape[0]):
for j in range(orders.shape[1]):
table.cell(i + 2, j).text = str(orders.values[i,j])
document.save('xxx.docx')
这个脚本运行良好,但是花费了很长的时间:写 1 行需要 10 秒。如果数据框有 5000 行,那就有问题了。有人知道更快的方法吗?
首先避免重复行和列查找,如下所示:
values = orders.values
for i in range(orders.shape[0]):
row = table.add_row()
for j, cell in enumerate(row.cells):
cell.text = str(values[i, j])
看看你得到了多少进步。
from docx import Document
from docx.shared import Pt
from docx.enum.table import WD_CELL_VERTICAL_ALIGNMENT
from docx.oxml.ns import qn
from docx.oxml import OxmlElement
import pandas as pd
# Create a new .docx document
doc = Document()
# Add a paragraph for the table title
doc.add_paragraph('Table Title', style='Heading1') # Modify the title text and style
# Add a table
table = doc.add_table(result_df.shape[0] + 1, result_df.shape[1]) # Create an empty table
# Set table formatting, including border thickness and dashed gridlines
for row in table.rows:
for cell in row.cells:
for paragraph in cell.paragraphs:
for run in paragraph.runs:
run.font.size = Pt(12) # Set font size
cell.vertical_alignment = WD_CELL_VERTICAL_ALIGNMENT.CENTER # Vertical center alignment
cell.paragraphs[0].alignment = 3 # Horizontal center alignment
# Add data to the table
for i in range(result_df.shape[0]):
for j in range(result_df.shape[1]):
table.cell(i + 1, j).text = str(result_df.iloc[i, j])
# Save the new .docx file
doc.save('output.docx')