import pandas as pd
# Specify the path to your Excel file
excel_file = "sales_jan_feb_mar_2021_d.xlsx"
try:
# Read the Excel file into a DataFrame
df = pd.read_excel(excel_file)
# Display the DataFrame in the terminal
print('-----')
print('Read excel data - all 9 columns')
print(df.to_string(index=False))
# Define the columns you want to read by name or index
columns_to_read = ['Customer', "Month", 'Actual_Sales', 'Salesperson'] # You can use column names
# Or you can use column indices (0-based) like columns_to_read = [0, 2]
# Read the specific columns from the Excel file
df = pd.read_excel(excel_file, usecols=columns_to_read)
# Display the DataFrame with the selected columns
print('-----')
print('Read specific columns of the excel data - 4 columns')
print(df.to_string(index=False))
def save_and_print_mean(data, group_by_column, output_file_name):
# Group by 'group_by_column' and calculate the mean of 'Actual_Sales'
mean_sales = data.groupby(group_by_column)['Actual_Sales'].mean().reset_index()
# Print the result
print('-----')
print(f'Actual_Sales Mean by {group_by_column}')
print(mean_sales)
# Save the result to an Excel file
mean_sales.to_excel(output_file_name, index=False)
print(f"Data has been saved to {output_file_name}")
save_and_print_mean(df, 'Salesperson', 'Actual_Sales Mean by Salesperson.xlsx')
save_and_print_mean(df, 'Customer', 'Actual_Sales Mean by Customer.xlsx')
save_and_print_mean(df, 'Month', 'Actual_Sales Mean by Month.xlsx')
except FileNotFoundError:
print(f"File '{excel_file}' not found.")
except pd.errors.EmptyDataError:
print(f"The Excel file '{excel_file}' is empty.")
except Exception as e:
print(f"An error occurred: {str(e)}")
-----
Read excel data - all 9 columns
Month Actual_Sales_Order_Date Budget_Sales Actual_Sales Actual_Sales_Order_Id Actual_Sales_Qty Sales_Comments Customer Salesperson
Feb 2021 1/2/21 900 1200 1009 100 Above budget Customer 1 Trifon Papadopoulos
Feb 2021 4/2/21 1200 1800 1010 160 Above budget Customer 2 George Dimitropoulos
Feb 2021 10/2/21 9000 7000 1011 670 Below budget Customer 3 Trifon Papadopoulos
Feb 2021 11/2/21 3200 3000 1012 295 Below budget Customer 4 George Dimitropoulos
Feb 2021 14/2/21 4400 3900 1013 372 Below budget Customer 5 Andreas Kyriakopoulos
Feb 2021 15/2/21 1800 2100 1014 195 Above budget Customer 6 Andreas Kyriakopoulos
Feb 2021 17/2/21 1900 2400 1015 220 Above budget Customer 7 Trifon Papadopoulos
Feb 2021 18/2/21 4100 4300 1016 385 Above budget Customer 8 Andreas Kyriakopoulos
Jan 2021 1/1/21 800 1000 1001 100 Above budget Customer 1 Trifon Papadopoulos
Jan 2021 3/1/21 1200 1500 1002 150 Above budget Customer 2 George Dimitropoulos
Jan 2021 4/1/21 8000 7000 1003 680 Below budget Customer 3 Trifon Papadopoulos
Jan 2021 7/1/21 3500 3000 1004 290 Below budget Customer 4 George Dimitropoulos
Jan 2021 9/1/21 4200 4000 1005 380 Below budget Customer 5 Andreas Kyriakopoulos
Jan 2021 12/1/21 1900 2000 1006 190 Above budget Customer 6 Andreas Kyriakopoulos
Jan 2021 17/1/21 1800 2300 1007 210 Above budget Customer 7 Trifon Papadopoulos
Jan 2021 19/1/21 4000 4200 1008 400 Above budget Customer 8 Andreas Kyriakopoulos
Mar 2021 1/3/21 1300 1100 1017 90 Below budget Customer 1 Trifon Papadopoulos
Mar 2021 10/3/21 1100 1700 1018 155 Above budget Customer 2 George Dimitropoulos
Mar 2021 11/3/21 8900 8000 1019 784 Below budget Customer 3 Trifon Papadopoulos
Mar 2021 12/3/21 2900 3100 1020 288 Above budget Customer 4 George Dimitropoulos
Mar 2021 17/3/21 4400 4150 1021 376 Below budget Customer 5 Andreas Kyriakopoulos
Mar 2021 25/3/21 1800 2150 1022 190 Above budget Customer 6 Andreas Kyriakopoulos
Mar 2021 26/3/21 1750 2250 1023 195 Above budget Customer 7 Trifon Papadopoulos
Mar 2021 27/3/21 4000 4450 1024 430 Above budget Customer 8 Andreas Kyriakopoulos
-----
Read specific columns of the excel data - 4 columns
Month Actual_Sales Customer Salesperson
Feb 2021 1200 Customer 1 Trifon Papadopoulos
Feb 2021 1800 Customer 2 George Dimitropoulos
Feb 2021 7000 Customer 3 Trifon Papadopoulos
Feb 2021 3000 Customer 4 George Dimitropoulos
Feb 2021 3900 Customer 5 Andreas Kyriakopoulos
Feb 2021 2100 Customer 6 Andreas Kyriakopoulos
Feb 2021 2400 Customer 7 Trifon Papadopoulos
Feb 2021 4300 Customer 8 Andreas Kyriakopoulos
Jan 2021 1000 Customer 1 Trifon Papadopoulos
Jan 2021 1500 Customer 2 George Dimitropoulos
Jan 2021 7000 Customer 3 Trifon Papadopoulos
Jan 2021 3000 Customer 4 George Dimitropoulos
Jan 2021 4000 Customer 5 Andreas Kyriakopoulos
Jan 2021 2000 Customer 6 Andreas Kyriakopoulos
Jan 2021 2300 Customer 7 Trifon Papadopoulos
Jan 2021 4200 Customer 8 Andreas Kyriakopoulos
Mar 2021 1100 Customer 1 Trifon Papadopoulos
Mar 2021 1700 Customer 2 George Dimitropoulos
Mar 2021 8000 Customer 3 Trifon Papadopoulos
Mar 2021 3100 Customer 4 George Dimitropoulos
Mar 2021 4150 Customer 5 Andreas Kyriakopoulos
Mar 2021 2150 Customer 6 Andreas Kyriakopoulos
Mar 2021 2250 Customer 7 Trifon Papadopoulos
Mar 2021 4450 Customer 8 Andreas Kyriakopoulos
-----
Actual_Sales Mean by Salesperson
Salesperson Actual_Sales
0 Andreas Kyriakopoulos 3472.222222
1 George Dimitropoulos 2350.000000
2 Trifon Papadopoulos 3583.333333
Data has been saved to Actual_Sales Mean by Salesperson.xlsx
-----
Actual_Sales Mean by Customer
Customer Actual_Sales
0 Customer 1 1100.000000
1 Customer 2 1666.666667
2 Customer 3 7333.333333
3 Customer 4 3033.333333
4 Customer 5 4016.666667
5 Customer 6 2083.333333
6 Customer 7 2316.666667
7 Customer 8 4316.666667
Data has been saved to Actual_Sales Mean by Customer.xlsx
-----
Actual_Sales Mean by Month
Month Actual_Sales
0 Feb 2021 3212.5
1 Jan 2021 3125.0
2 Mar 2021 3362.5
Data has been saved to Actual_Sales Mean by Month.xlsx
import openpyxl
from openpyxl import load_workbook
# Specify the Excel file name
excel_file = "sales_jan_feb_mar_2021_d.xlsx"
# Load the Excel file and create a workbook object
workbook = load_workbook(excel_file)
# Specify the name of the sheet you want to delete
sheet_name_to_delete = "ActSalesMean by Salesperson" # Replace with the actual sheet name
# Check if the sheet exists, and if so, delete it
if sheet_name_to_delete in workbook.sheetnames:
sheet_to_delete = workbook[sheet_name_to_delete]
workbook.remove(sheet_to_delete)
# Save the modified workbook
workbook.save(excel_file)
# Create now an ExcelWriter in 'append' mode
writer = pd.ExcelWriter(excel_file, engine='openpyxl', mode='a')
data = pd.read_excel(excel_file)
mean_sales_by_salesperson = data.groupby('Salesperson')['Actual_Sales'].mean().reset_index()
# Rename the 'old_label' column to 'new_label'
mean_sales_by_salesperson = mean_sales_by_salesperson.rename(columns={'Actual_Sales': 'Actual_Sales_Mean'})
# Write the DataFrame to a new sheet
mean_sales_by_salesperson.to_excel(writer, sheet_name='ActSalesMean by Salesperson', index=False)
# Save and close the ExcelWriter
writer.save()
print(f"Data has been saved to {excel_file} in the 'ActSalesMean by Salesperson' tab.")
print(mean_sales_by_salesperson.to_string(index=False))
# plot the dataframe
from matplotlib import pyplot as plt
import seaborn as sns
sns.set_style("dark")
mean_sales_by_salesperson.plot(x="Salesperson", y=["Actual_Sales_Mean"], kind="bar")
plt.rcParams["figure.figsize"] = [11.50, 7.50]
plt.rcParams["figure.autolayout"] = True
plt.xticks(rotation=30, horizontalalignment="center")
plt.title("Sales / Jan 21 - Mar 21")
plt.xlabel("Salesperson")
plt.ylabel("Actual Sales Mean")
plt.legend(["Actual Sales Mean"])
# print bar graph
plt.show()
Data has been saved to sales_jan_feb_mar_2021_d.xlsx in the 'ActSalesMean by Salesperson' tab.
Salesperson Actual_Sales_Mean
Andreas Kyriakopoulos 3472.222222
George Dimitropoulos 2350.000000
Trifon Papadopoulos 3583.333333
#from openpyxl import load_workbook
# Specify the Excel file name
excel_file = "sales_jan_feb_mar_2021_d.xlsx"
# Load the Excel file and create a workbook object
workbook = load_workbook(excel_file)
# Specify the name of the sheet you want to delete
sheet_name_to_delete = "ActSalesMean by Customer" # Replace with the actual sheet name
# Check if the sheet exists, and if so, delete it
if sheet_name_to_delete in workbook.sheetnames:
sheet_to_delete = workbook[sheet_name_to_delete]
workbook.remove(sheet_to_delete)
# Save the modified workbook
workbook.save(excel_file)
# Create now an ExcelWriter in 'append' mode
writer = pd.ExcelWriter(excel_file, engine='openpyxl', mode='a')
data = pd.read_excel(excel_file)
mean_sales_by_customer = data.groupby('Customer')['Actual_Sales'].mean().reset_index()
# Rename the 'old_label' column to 'new_label'
mean_sales_by_customer = mean_sales_by_customer.rename(columns={'Actual_Sales': 'Actual_Sales_Mean'})
# Write the DataFrame to a new sheet
mean_sales_by_customer.to_excel(writer, sheet_name='ActSalesMean by Customer', index=False)
# Save and close the ExcelWriter
writer.save()
print(f"Data has been saved to {excel_file} in the 'ActSalesMean by Customer' tab.")
print(mean_sales_by_customer.to_string(index=False))
sns.set_style("dark")
mean_sales_by_customer.plot(x="Customer", y=["Actual_Sales_Mean"], kind="bar")
plt.rcParams["figure.figsize"] = [11.50, 7.50]
plt.rcParams["figure.autolayout"] = True
plt.xticks(rotation=30, horizontalalignment="center")
plt.title("Sales / Jan 21 - Mar 21")
plt.xlabel("Customer")
plt.ylabel("Actual Sales Mean")
plt.legend(["Actual Sales Mean"])
# print bar graph
plt.show()
Data has been saved to sales_jan_feb_mar_2021_d.xlsx in the 'ActSalesMean by Customer' tab. Customer Actual_Sales_Mean Customer 1 1100.000000 Customer 2 1666.666667 Customer 3 7333.333333 Customer 4 3033.333333 Customer 5 4016.666667 Customer 6 2083.333333 Customer 7 2316.666667 Customer 8 4316.666667
#from openpyxl import load_workbook
# Specify the Excel file name
excel_file = "sales_jan_feb_mar_2021_d.xlsx"
# Load the Excel file and create a workbook object
workbook = load_workbook(excel_file)
# Specify the name of the sheet you want to delete
sheet_name_to_delete = "ActSalesMean by Month" # Replace with the actual sheet name
# Check if the sheet exists, and if so, delete it
if sheet_name_to_delete in workbook.sheetnames:
sheet_to_delete = workbook[sheet_name_to_delete]
workbook.remove(sheet_to_delete)
# Save the modified workbook
workbook.save(excel_file)
# Create now an ExcelWriter in 'append' mode
writer = pd.ExcelWriter(excel_file, engine='openpyxl', mode='a')
data = pd.read_excel(excel_file)
mean_sales_by_month = data.groupby('Month')['Actual_Sales'].mean().reset_index()
# Rename the 'old_label' column to 'new_label'
mean_sales_by_month = mean_sales_by_month.rename(columns={'Actual_Sales': 'Actual_Sales_Mean'})
# Write the DataFrame to a new sheet
mean_sales_by_month.to_excel(writer, sheet_name='ActSalesMean by Month', index=False)
# Save and close the ExcelWriter
writer.save()
print(f"Data has been saved to {excel_file} in the 'Act Sales Mean by Month' tab.")
print(mean_sales_by_month.to_string(index=False))
sns.set_style("dark")
mean_sales_by_month.plot(x="Month", y=["Actual_Sales_Mean"], kind="bar")
plt.rcParams["figure.figsize"] = [11.50, 7.50]
plt.rcParams["figure.autolayout"] = True
plt.xticks(rotation=30, horizontalalignment="center")
plt.title("Sales / Jan 21 - Mar 21")
plt.xlabel("Month")
plt.ylabel("Actual Sales Mean")
plt.legend(["Actual Sales Mean"])
# print bar graph
plt.show()
Data has been saved to sales_jan_feb_mar_2021_d.xlsx in the 'Act Sales Mean by Month' tab. Month Actual_Sales_Mean Feb 2021 3212.5 Jan 2021 3125.0 Mar 2021 3362.5
# Styling three sheets in xlsx file sales_jan_feb_mar_2021_d.xlsx
from openpyxl.styles import PatternFill, Border, Side, colors, Alignment, Protection, Font, Color
from openpyxl.worksheet.dimensions import ColumnDimension, DimensionHolder
from openpyxl.utils import get_column_letter
font2 = Font(name='Calibri', size=17, bold=True, color='07101c')
font3 = Font(name='Calibri', size=13, bold=False, color='07101c')
import warnings
warnings.filterwarnings('ignore', category=UserWarning, module='openpyxl')
# Styling xlsx file sales_jan_feb_mar_2021_d.xlsx
wb = openpyxl.load_workbook('sales_jan_feb_mar_2021_d.xlsx')
# Styling sheet Actual Sales by Salesperson
ws = wb['ActSalesMean by Salesperson']
dim_holder = DimensionHolder(worksheet=ws)
for col in range(ws.min_column, ws.max_column+1):
dim_holder[get_column_letter(col)] = ColumnDimension(ws, min=col, max=col, width=37)
ws.column_dimensions = dim_holder
for cell in ws['A']:
cell.alignment = Alignment(horizontal="center")
cell.font = font3
for cell in ws['B']:
cell.alignment = Alignment(horizontal="center")
cell.font = font3
# Styling sheet Actual Sales by Customer
ws = wb['ActSalesMean by Customer']
dim_holder = DimensionHolder(worksheet=ws)
for col in range(ws.min_column, ws.max_column+1):
dim_holder[get_column_letter(col)] = ColumnDimension(ws, min=col, max=col, width=37)
ws.column_dimensions = dim_holder
for cell in ws['A']:
cell.alignment = Alignment(horizontal="center")
cell.font = font3
for cell in ws['B']:
cell.alignment = Alignment(horizontal="center")
cell.font = font3
# Styling sheet Actual Sales by Month
ws = wb['ActSalesMean by Month']
dim_holder = DimensionHolder(worksheet=ws)
for col in range(ws.min_column, ws.max_column+1):
dim_holder[get_column_letter(col)] = ColumnDimension(ws, min=col, max=col, width=37)
ws.column_dimensions = dim_holder
for cell in ws['A']:
cell.alignment = Alignment(horizontal="center")
cell.font = font3
for cell in ws['B']:
cell.alignment = Alignment(horizontal="center")
cell.font = font3
wb.save('sales_jan_feb_mar_2021_d.xlsx')
print("You are done styling sheets 'ActSalesMean by Salesperson', 'ActSalesMean by Customer' and 'ActSalesMean by Month' in 'sales_jan_feb_mar_2021_d.xlsx'")
You are done styling sheets 'ActSalesMean by Salesperson', 'ActSalesMean by Customer' and 'ActSalesMean by Month' in 'sales_jan_feb_mar_2021_d.xlsx'