失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > python批量读取csv并写入_Python如何批量读取CSV文件中指定信息并写入doc文件命名中?...

python批量读取csv并写入_Python如何批量读取CSV文件中指定信息并写入doc文件命名中?...

时间:2023-04-10 16:53:43

相关推荐

python批量读取csv并写入_Python如何批量读取CSV文件中指定信息并写入doc文件命名中?...

提供思路自己完成吧

1、用 os.walk 加 if 判断将所有的csv 文件名读到list中

用glob 也可以

import glob

for x in glob.glob('*.csv'):

print(x)

2、用csv 库或 第三方库 panpds 循环处理上面list中的文件

3、用doc 库 写doc 文件。

pip install python-docx

import docx

import pandas as pd

# i am not sure how you are getting your data, but you said it is a

# pandas data frame

df = pd.DataFrame(data)

# open an existing document

doc = docx.Document('./test.docx')

# 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])

# 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])

# save the doc

doc.save('./test.docx')

如果觉得《python批量读取csv并写入_Python如何批量读取CSV文件中指定信息并写入doc文件命名中?...》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。