实战
安装模块 pip install openpyxl
第一步:创建表格及工作表
from openpyxl import Workbook
# 创建一个表格
wb = Workbook()
# 创建工作表
one = wb.create_sheet('娃哈哈')
two = wb.create_sheet('旺仔')
wb.create_sheet('牛奶')
three = wb.create_sheet('Tony老师')
wb.create_sheet('在上课')
# 保存
wb.save('西游记.xlsx')

第二步:添加表格数据
# 添加一个值 单元格
one.cell(row=15,column=1,value='老师')
#
one['G23'] = 123
one['a2'] = 2343
one['C3'] = 'hahaha'
a = [11,2,3,4,5,6,7,8,'猴子','妲己']
# 多行添加
two.append(a)
# 多行 多列添加
data = [
['入职时间','公司人力成本总额','在岗人数','人均成本'],
[2015,10000,1000,9.6],
[2016,12000,1100,19.6],
[2017,15000,1500,29.6],
[2018,9000,800,39.6],
[2019,11000,900,9.6],
[2020,30000,2000,16.6],
[2021,20000,1700,10.1],
]
for tony in data:
three.append(tony)



第三步:数据可视化分析
from openpyxl.chart import PieChart,Reference,BarChart
# 数据分析 - 饼状图
pie = PieChart()
pie.title='Tony老师来到此一游'
# 设置数据对比 展示 图标
label = Reference(three,min_col=1,min_row=2,max_row=8)
data = Reference(three,min_col=2,min_row=2,max_row=8)
pie.add_data(data)
pie.set_categories(label)
# 添加
three.add_chart(pie,'b10')

# 数据分析 - 柱状图
bar = BarChart()
bar.title='Tony老师来到此一游'
bar.x_axis.title= '年份'
bar.type = 'bar'
bar.style = 15
# 设置数据对比 展示 图标
labels = Reference(three,min_col=1,min_row=2,max_row=8)
datas = Reference(three,min_col=2,min_row=2,max_row=8)
bar.add_data(datas)
bar.set_categories(labels)
# 添加
three.add_chart(bar,'j2')
效果展示

在这个浮躁的时代;竟然还有人能坚持篇篇原创;
如果本文对你学习有所帮助-可以点赞👍+ 关注!将持续更新更多新的文章。
支持原创。感谢!