import openpyxl
from openpyxl import Workbook
from openpyxl.chart import (
Reference,
Series,
AreaChart3D
)
wb = openpyxl.load_workbook(filename=r'./办公自动化/files/test.xlsx')
sheet_names = wb.sheetnames
print(f"Original sheet names: {sheet_names}")
ws = wb[sheet_names[3]]
ws.title = 'areaChart3D'
rows = [
['团队名称', 'Q1', 'Q2', 'Q3'],
['精英队', 1200, 1800, 2200],
['王者队', 1500, 2000, 2500],
['野战队', 1000, 2200, 3000],
['虎狼队', 1100, 1650, 2550],
['战狼队', 1150, 1700, 2650],
['金牌队', 1200, 1950, 3150],
['无敌队', 1050, 1700, 2730]
]
ws.delete_rows(1, ws.max_row)
for row in rows:
ws.append(row)
chart = AreaChart3D()
chart.title = '各团队每季度销售业绩3D对比图(单位:万元)'
chart.style = 10
chart.x_axis.title = 'Team name'
chart.y_axis.title = 'Sales volume'
chart.width = 20
chart.height = 13
chart.legend = None
cats = Reference(ws, min_col=1, min_row=1, max_row=8)
data = Reference(ws, min_col=2, max_col=4, min_row=1, max_row=8)
chart.set_categories(cats)
chart.add_data(data, titles_from_data=True)
ws.add_chart(chart, 'F1')
wb.save('./办公自动化/files/test.xlsx')