Kirish I. Umumiy qism
Python funktsiyasi yordamida hisobotni avtomatlashtirish
Download 1.09 Mb.
|
Kurs ishi
- Bu sahifa navigatsiya:
- """The file name should have the following structure: sales_month.xlsx"""
Python funktsiyasi yordamida hisobotni avtomatlashtirish
Endi hisobot tayyor bo'lgach, biz barcha kodlarimizni hisobot yaratishni avtomatlashtiradigan funktsiyaga qo'yishimiz mumkin. Va keyingi safar biz bunday hisobotni yaratmoqchi bo'lganimizda, biz faqat fayl nomini kiritishimiz va kodni ishga tushirishimiz kerak bo'ladi. Eslatma. Ushbu funktsiya ishlashi uchun fayl nomi "sales_month.xlsx». Bundan tashqari, biz savdo faylining oy/yilini o'zgaruvchi sifatida ishlatadigan bir nechta kod satrlarini qo'shdik, shunda biz uni yakuniy fayl va hisobot sarlavhasida qayta ishlatishimiz mumkin. Quyidagi kod qo'rqinchli tuyulishi mumkin, ammo bu biz yuqorida yozgan barcha narsalarni birlashtirishdir. Bundan tashqari, yangi o'zgaruvchilar file_namemonth_nameva month_and_extension. import pandas as pd import openpyxl from openpyxl import load_workbook from openpyxl.styles import Font from openpyxl.chart import BarChart, Reference import string def automate_excel(file_name): """The file name should have the following structure: sales_month.xlsx""" # read excel file excel_file = pd.read_excel(file_name) # make pivot table report_table = excel_file.pivot_table(index='Gender', columns='Product line', values='Total', aggfunc='sum').round(0) # splitting the month and extension from the file name month_and_extension = file_name.split('_')[1] # send the report table to excel file report_table.to_excel(f'report_{month_and_extension}', sheet_name='Report', startrow=4) # loading workbook and selecting sheet wb = load_workbook(f'report_{month_and_extension}') sheet = wb['Report'] # cell references (original spreadsheet) min_column = wb.active.min_column max_column = wb.active.max_column min_row = wb.active.min_row max_row = wb.active.max_row # adding a chart archart = BarChart() data = Reference(sheet, min_col=min_column+1, max_col=max_column, min_row=min_row, max_row=max_row) #including headers categories = Reference(sheet, min_col=min_column, max_col=min_column, min_row=min_row+1, max_row=max_row) #not including headers barchart.add_data(data, titles_from_data=True) barchart.set_categories(categories) sheet.add_chart(barchart, "B12") #location chart barchart.title = 'Sales by Product line' barchart.style = 2 #choose the chart style # applying formulas # first create alphabet list as references for cells alphabet = list(string.ascii_uppercase) excel_alphabet = alphabet[0:max_column] #note: Python lists start on 0 -> A=0, B=1, C=2. #note2 the [a:b] takes b-a elements # sum in columns B-G Download 1.09 Mb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling