读取csv文件的内容写入word里面
程序员文章站
2022-06-22 09:25:44
读取csv写入word代码:#!/usr?bin/env/ python# -*- coding:utf-8 -*-# author: lai zheng laing# datetime: 2020/10/23 15:53# software: PyCharmimport docximport csvfile_path = 'E:/The_data_for_topiclcrlaw/yi_qing_fangkong_data/爬取的疫情防控数据.csv'with open(file_pat...
读取csv写入word
代码:
#!/usr?bin/env/ python
# -*- coding:utf-8 -*-
# author: lai zheng laing
# datetime: 2020/10/23 15:53
# software: PyCharm
import docx
import csv
file_path = 'E:/The_data_for_topiclcrlaw/yi_qing_fangkong_data/爬取的疫情防控数据.csv'
with open(file_path,encoding='utf-8') as file_name: # 打开文件所在的位置
reader =csv.reader(file_name)
result = list(reader) # 存入列表
del result[0]
# str_convert = ''.join(result[0][:])
# print(str_convert)
a = (len(result[:][:]))
b = (len(result[0][:]))
doc = docx.Document() # 创建一个空的word
for i in range(a):
str_convert = ''.join(result[i][:]) # 将列表的内容写入字符串
str_num = ''.join(str_convert) # 创建一个总的字符串
doc.add_paragraph(str_num) # 写入word
print(str_num)
doc.save('C:/Users/acer/Desktop/test.doc') # 保存word
## 结果:
本文地址:https://blog.csdn.net/qq_42794767/article/details/109256494