-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4-add_chart.py
More file actions
42 lines (34 loc) · 942 Bytes
/
4-add_chart.py
File metadata and controls
42 lines (34 loc) · 942 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from openpyxl import load_workbook
from openpyxl.chart import BarChart, Reference
# 1 - Ler pasta de trabalho e planilha
wb = load_workbook("data/pivot_table.xlsx")
sheet = wb["Relatorio"]
# 2 - Referências das linhas e colunas
min_column = wb.active.min_column
max_column = wb.active.max_column
min_row = wb.active.min_row
max_row = wb.active.max_row
# 3 - Adicionando dados e categorias no gráfico
barchart = BarChart()
data = Reference(
sheet,
min_col = min_column + 1,
max_col = max_column,
min_row = min_row,
max_row = max_row
)
categories = Reference(
sheet,
min_col = min_column,
max_col = max_column,
min_row = min_row + 1,
max_row = max_row
)
barchart.add_data(data, titles_from_data = True)
barchart.set_categories(categories)
# 4 - Criando o gráfico
sheet.add_chart(barchart, "B10")
barchart.title = "Vendas por fabricantes"
barchart.style = 2
# 5 - Salvando o workbook
wb.save("data/barchart.xlsx")