-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4-add_chart.py
42 lines (34 loc) · 963 Bytes
/
4-add_chart.py
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
# Ler pasta de trabalho e planilha
wb = load_workbook('data/pivot_table.xlsx')
sheet = wb['Relatorio']
# Referencias 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
# Adicionar dados e categorias no grafico
barchart = BarChart()
data = Reference(
sheet,
min_col = min_column + 1, # Sem o Ano
max_col = max_column,
min_row = min_row,
max_row = max_row
)
categories = Reference(
sheet,
min_col = min_column,
max_col = min_column,
min_row = min_row + 1, # Sem o cabecalho
max_row = max_row
)
barchart.add_data(data, titles_from_data = True)
barchart.set_categories(categories)
# Criar o grafico
sheet.add_chart(barchart, 'B10')
barchart.title = 'Vendas por Fabricantes'
barchart.style = 2
# Salvar o workbook
wb.save('data/barchart.xlsx')