from docx import Document
import pandas as pd
import matplotlib.pyplot as plt
tata2=pd.read_excel("jjh.xlsx")#打开Excel
tata2.sort_values(by="Age",inplace=True,ascending=False)#对age里的数据排序
plt.bar(tata2.Name,tata2.Age,color="orange")#柱状图显示name和age
plt.title("jjh",fontsize=18)
plt.xlabel("tata1")
plt.ylabel("values")#两个坐标轴
plt.xticks(tata2.Name,rotation="90")#字体旋转90度
plt.tight_layout()#紧凑型
image="data.jpg"
plt.savefig(image)
document=Document()
first_student=tata2.iloc[0,:]["Name"]
first_score=tata2.iloc[0,:]["Score"]
p=document.add_paragraph("分数排名第一的学生是")
p.add_run(str(first_student)).bold=True
p.add_run(",他的分数是")
p.add_run(str(first_score)).bold=True
p=document.add_paragraph(f"总共有{len(tata2.Name)}")
table=document.add_table(rows=len(tata2.Name)+1,cols=2)
document.add_heading("周报告",level=1)
table.style="LightShading-Accent1"
table.cell(0,0).text="学生名称"
table.cell(0,1).text="学生分数"
for i,(index,row) in enumerate(tata2.iterrows()):
table.cell(i+1,0).text=str(row['Name'])
table.cell(i+1,1).text = str(row['Score'])
document.add_picture(image)
document.save("s.docx")
print("done")
excel写到Word
最新推荐文章于 2023-12-11 17:42:28 发布