python自动化办公(第10课)编辑生成Excal内图表
- openpyxl插入图片
- openpyxl.drawing.image
- sheet.add_image()
from openpyxl import load_workbook
from openpyxl.drawing.image import Image
workbook = load_workbook(filename="新建表.xlsx")
sheet = workbook.active
logo = Image("裴擒虎.png")
logo.height = 100
logo.width = 100
sheet.add_image(logo,"D3")
workbook.save(filename="新建表.xlsx")
- 插入柱状图
from openpyxl import load_workbook
from openpyxl.chart import BarChart,Reference
workbook = load_workbook(filename="新建表.xlsx")
sheet = workbook.active
chart = BarChart()
data = Reference(worksheet=sheet,min_row=12,max_row=14,min_col=3,max_col=14)
categories = Reference(sheet,min_col=2,min_row=13,max_row=14)
chart.add_data(data,titles_from_data=True)
chart.set_categories(categories)
sheet.add_chart(chart,"G2")
workbook.save(filename="新建表.xlsx")
from openpyxl import load_workbook
from openpyxl.chart import BarChart,Reference
workbook = load_workbook(filename="新建表.xlsx")
sheet = workbook.active
chart = BarChart()
data = Reference(worksheet=sheet,min_row=32,max_row=38,min_col=3,max_col=4)
categories = Reference(sheet,min_col=2,min_row=32,max_row=38)
chart.add_data(data,titles_from_data=True)
chart.set_categories(categories)
sheet.add_chart(chart,"G32")
workbook.save(filename="新建表.xlsx")
- 插入条形图
from openpyxl import load_workbook
from openpyxl.chart import LineChart,Reference
workbook = load_workbook(filename="新建表.xlsx")
sheet = workbook.active
chart = LineChart()
data = Reference(worksheet=sheet,min_row=13,max_row=14,min_col=2,max_col=14)
categories = Reference(sheet,min_col=3,max_col=14,min_row=12)
chart.add_data(data,from_rows=True,titles_from_data=True)
chart.set_categories(categories)
sheet.add_chart(chart,"F1")
workbook.save(filename="新建表.xlsx")
作业:编写python程序,要求如下:
- 打开文件阿里云天池电商婴儿用户数据.xlsx
- 统计buy_mount数据
- 统计buy_mount的数量绘制一个柱状图和一个饼状图
- 保存Excel文件
这题我不会,不写了,正宗数据可视化还是找专业课程学习吧,这个老师这里怎么操作也没讲清楚,会写的可以私信让我白嫖