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

这篇博客介绍了如何利用Python的os和glob模块查找并读取所有CSV文件,然后使用csv和pandas库对数据进行处理。接着,通过python-docx库将数据写入新的DOCX文档中,创建表格并填充数据帧内容,从而实现CSV到DOCX的转换。
摘要由CSDN通过智能技术生成

提供思路自己完成吧

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')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值