excel 饼状图绘制
案例1
from openpyxl import Workbook
from openpyxl.chart import PieChart, Reference
from openpyxl.chart.marker import DataPoint
wb = Workbook()
sheet = wb.active
data = [
['Pie', 'Sold'],
['苹果', 50, 50, 50, 50, 50],
['樱桃', 30, 50, 40, 40, 10],
['南瓜', 10, 50, 30, 30, 20],
['巧克力', 40, 50, 20, 30, 30],
]
for i in data:
sheet.append(i)
pie = PieChart()
pie.title = "商店的销售"
data = Reference(worksheet=sheet, min_col=2, min_row=1, max_row=len(data))
lables = Reference(worksheet=sheet, min_col=1, min_row=2, max_row=len(data))
pie.add_data(data, titles_from_data=True)
pie.set_categories(lables)
slice = DataPoint(idx=0, explosion=50)
pie.series[0].data_points = [slice]
print(pie.series)
sheet.add_chart(pie, f'A{len(data) + 3}')
wb.save("hello.xlsx")
案例2
from openpyxl import Workbook
wb = Workbook()
sheet = wb.active
data = [
['门店', '售卖量'],
['金燕龙店', 110],
['昌平店', 140],
['顺义店', 188]
]
for i in data:
sheet.append(i)
from openpyxl.chart import PieChart, PieChart3D, Reference
pie = PieChart()
pie = PieChart3D()
pie.title = "2020年北京营业额"
data = Reference(sheet, min_col=2, min_row=1, max_row=len(data))
lables = Reference(sheet, min_col=1, min_row=2, max_row=len(data))
pie.add_data(data, titles_from_data=True)
pie.set_categories(lables)
sheet.add_chart(pie, anchor="D1")
wb.save("hello.xlsx")
excel 折线图绘制
案例1
from datetime import date
from openpyxl import Workbook
from openpyxl.chart import LineChart, Reference
wb = Workbook()
sheet = wb.active
sheet.column_dimensions['A'].width = 15
rows = [
['日期', 'python 1', 'python 2', 'python 3'],
[date(2015, 9, 1), 40, 30, 25],
[date(2015, 9, 2), 40, 25, 30],
[date(2015, 9, 3), 50, 30, 45],
[date(2015, 9, 4), 30, 25, 40],
[date(2015, 9, 5), 25, 35, 30],
[date(2015, 9, 6), 20, 40, 35],
]
for data in rows:
sheet.append(data)
linechart = LineChart()
linechart.title = 'python版本的占比'
linechart.style = 13
linechart.x_axis.title = '数字'
linechart.y_axis.title = '占比大小'
data = Reference(worksheet=sheet, min_col=2, min_row=1, max_col=4, max_row=7)
linechart.add_data(data, titles_from_data=True)
"""
'circle', 'dash', 'diamond', 'dot', 'picture',
'plus', 'square', 'star', 'triangle', 'x', 'auto'
"""
line1 = linechart.series[0]
line1.marker.symbol = "x"
line1.marker.graphicalProperties.solidFill = "FF0000"
line1.marker.graphicalProperties.line.solidFill = "FF0000"
line1.graphicalProperties.line.noFill = True
line2 = linechart.series[1]
line2.graphicalProperties.solidFill = "00AAAA"
"""
'solid', 'dot', 'dash', 'lgDash', 'dashDot',
'lgDashDot', 'lgDashDotDot', 'sysDash', 'sysDot',
'sysDashDot','sysDashDotDot'
"""
line2.graphicalProperties.line.dashStyle = "sysDot"
from openpyxl.drawing.line import LineProperties
print(line2.graphicalProperties.line)
line2.graphicalProperties.line.width = 100050
line3 = linechart.series[2]
line3.smooth = True
sheet.add_chart(linechart, 'A10')
wb.save("hello.xlsx")
案例2
from random import randint
from datetime import time
from openpyxl import Workbook
from openpyxl.chart import LineChart, Reference, LineChart3D
wb = Workbook()
sheet = wb.active
rows = [
["时间", "服务人数"],
]
for i in range(8, 24):
rows.append([time(i), randint(0, 200)])
for data in rows:
sheet.append(data)
linechart = LineChart()
linechart = LineChart3D()
linechart.title = "客服绩效图当天"
linechart.y_axis.title = "服务人数"
linechart.x_axis.title = "时间"
data = Reference(worksheet=sheet,
min_col=2,
min_row=1,
max_col=2,
max_row=len(rows), )
linechart.add_data(data, titles_from_data=True)
linechart.x_axis.number_format = "HH:MM"
x_title = Reference(worksheet=sheet, min_col=1, min_row=2, max_row=len(rows))
linechart.set_categories(x_title)
sheet.add_chart(linechart, "A20")
wb.save("hello.xlsx")
坐标轴中可能涉及到的日期格式
from datetime import datetime
date_one_str = "2020-12-19"
date_two_str = "2020年12月19日"
date_three_str = "2020/12/19"
today_date = datetime.today()
today_date.strftime('%Y-%m-%d %H:%M:%S')
today_date.strftime('%Y年%m月%d日')
today_date.strftime('%Y/%m/%d')
datetime.strptime(date_one_str, "%Y-%m-%d")
datetime.strptime(date_two_str, "%Y年%m月%d日")
datetime.strptime(date_three_str, "%Y/%m/%d")