forked from thepycoach/automation
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ed2165f
commit 3c62a7f
Showing
1 changed file
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import pandas as pd | ||
from openpyxl import load_workbook | ||
from openpyxl.chart import BarChart, Reference | ||
from openpyxl.styles import Font | ||
import string | ||
|
||
def automatizar_excel(nombre_archivo): | ||
"""Input sales_mes.xlsx / Output report_mes.xlsx""" | ||
archivo_excel = pd.read_excel(nombre_archivo) | ||
tabla_pivote = archivo_excel.pivot_table(index='Gender', columns='Product line', values='Total', aggfunc='sum').round(0) | ||
mes_extension = nombre_archivo.split('_')[1] | ||
tabla_pivote.to_excel(f'sales_{mes_extension}', startrow=4, sheet_name='Report') | ||
|
||
wb = load_workbook(f'sales_{mes_extension}') | ||
pestaña = wb['Report'] | ||
|
||
min_col = wb.active.min_column | ||
max_col = wb.active.max_column | ||
min_fila = wb.active.min_row | ||
max_fila = wb.active.max_row | ||
|
||
# grafico | ||
barchart = BarChart() | ||
|
||
data = Reference(pestaña, min_col=min_col+1, max_col=max_col, min_row=min_fila, max_row=max_fila) | ||
categorias = Reference(pestaña, min_col=min_col, max_col=min_col, min_row=min_fila+1, max_row=max_fila) | ||
|
||
barchart.add_data(data, titles_from_data=True) | ||
barchart.set_categories(categorias) | ||
|
||
pestaña.add_chart(barchart, 'B12') | ||
barchart.title = 'Ventas' | ||
barchart.style = 2 | ||
|
||
abecedario = list(string.ascii_uppercase) | ||
abecedario_excel = abecedario[0:max_col] | ||
|
||
for i in abecedario_excel: | ||
if i!='A': | ||
pestaña[f'{i}{max_fila+1}'] = f'=SUM({i}{min_fila+1}:{i}{max_fila})' | ||
pestaña[f'{i}{max_fila+1}'].style = 'Currency' | ||
|
||
pestaña[f'{abecedario_excel[0]}{max_fila+1}'] = 'Total' | ||
|
||
|
||
pestaña['A1'] = 'Reporte' | ||
mes = mes_extension.split('.')[0] | ||
pestaña['A2'] = mes | ||
|
||
pestaña['A1'].font = Font('Arial', bold=True, size=20) | ||
pestaña['A2'].font = Font('Arial', bold=True, size=12) | ||
|
||
wb.save(f'sales_{mes_extension}') | ||
return | ||
|
||
# automatizar reporte 2021 | ||
automatizar_excel('sales_2021.xlsx') | ||
|
||
# automatizar reportes mensuales | ||
automatizar_excel('sales_enero.xlsx') | ||
automatizar_excel('sales_febrero.xlsx') | ||
automatizar_excel('sales_marzo.xlsx') |