Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
thepycoach authored May 14, 2021
1 parent ed2165f commit 3c62a7f
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions spanish/Automatizar Excel (formula).py
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')

0 comments on commit 3c62a7f

Please sign in to comment.