【发布时间】:2021-11-05 00:10:11
【问题描述】:
我必须在 docx word 文档中从 CSV 文件中添加一个大约 1500 行和 9 列(75 页)的表格。使用 python-docx。
我尝试了不同的方法,用 pandas 读取 csv 或直接打开 de csv 文件,我花了大约 150 分钟以我选择的方式独立完成工作
我的问题是这是否是正常行为,或者是否存在任何其他改进此任务的方法。
我正在使用这个 for 循环来读取几个 cvs 文件并以表格格式解析它
for toTAB in listBRUTO:
df= pd.read_csv(toTAB)
# add a table to the end and create a reference variable
# extra row is so we can add the header row
t = doc.add_table(df.shape[0]+1, df.shape[1])
t.style = 'LightShading-Accent1' # border
# add the header rows.
for j in range(df.shape[-1]):
t.cell(0,j).text = df.columns[j]
# add the rest of the data frame
for i in range(df.shape[0]):
for j in range(df.shape[-1]):
t.cell(i+1,j).text = str(df.values[i,j])
#TABLE Format
for row in t.rows:
for cell in row.cells:
paragraphs = cell.paragraphs
for paragraph in paragraphs:
for run in paragraph.runs:
font = run.font
font.name = 'Calibri'
font.size= Pt(7)
doc.add_page_break()
doc.save('blabla.docx')
提前致谢
【问题讨论】:
-
你做了什么花了这么长时间的尝试?
-
从服务器中提取数据,例如:工作流程
-
我的意思是你使用的代码是什么。如果您不发布您的最小复制代码,我们只是在猜测。
-
你是对的,只是添加了一些代码来更好地理解我的问题