# encoding=gbkimport os
import pandas as pd
from docx import Document
head = pd.read_excel('./excel数据源/'+os.listdir('./excel数据源/')[0],dtype=str,keep_default_na="").head(0)
head =list(head)print(head)
df = pd.read_excel('./excel数据源/'+os.listdir('./excel数据源/')[0],sheet_name=0,dtype=str,keep_default_na="")
index =0# 创建一个新的docx文档
document = Document()
p = document.add_paragraph()for d in df.values.tolist():for h in head:if index<=len(head)-2:print(h+":"+d[index])# 添加一个段落# document.add_paragraph(h+":"+d[index])
p.add_run(h+":"+d[index]+"\n")
index+=1# print(index)else:
index=0
p.add_run("\n")print("-"*50)# 保存文档
document.save('./excel转docx结果/res.docx')