First, let's understand the prerequisites. You will need to have Python installed on your computer. Then, you should have packages such as pandas
and xlsxwriter
installed. To install these packages, you can use pip (a package installer for Python). In your terminal, type:
pip install pandas xlsxwriter
Now, let's dive into how to automate Excel report generation with Python. We'll use a hypothetical sales report, which includes calculations such as totals and percentages.
Step 1: Importing required libraries
import pandas as pd
Here, we import pandas
, a library offering data structures and operations for manipulating numerical tables and time series.
Step 2: Preparing the data
Ask your specific question in Mate AI
In Mate you can connect your project, ask questions about your repository, and use AI Agent to solve programming tasks
Load the data from an existing .csv or .xlsx file or prepare the data manually.
# Loading data from a .csv file
df = pd.read_csv('sales.csv')
# Same can be done for .xlsx file
# df = pd.read_excel('sales.xlsx')
Now, let's assume that our DataFrame df
looks like this:
product | units | price | revenue | |
---|---|---|---|---|
0 | A | 10 | 20 | 200 |
1 | B | 5 | 50 | 250 |
2 | C | 8 | 30 | 240 |
Step 3: Perform necessary calculations
For example, let's calculate total units sold, total revenue and revenue percentage for each product.
total_units = df['units'].sum()
total_revenue = df['revenue'].sum()
df['revenue_percentage'] = df['revenue']/total_revenue * 100
Step 4: Create a new Excel writer object
writer = pd.ExcelWriter('sales_report.xlsx', engine='xlsxwriter')
Here, we create a new Excel file named 'sales_report.xlsx' through the ExcelWriter object of pandas.
Step 5: Write data to Excel file
df.to_excel(writer, sheet_name = 'Sales Report', index = False)
Step 6: Apply formatting
xlsxwriter
allows you to format your Excel files. For instance, you can add charts, colors, fonts etc.
# Get xlsxwriter objects
workbook = writer.book
worksheet = writer.sheets['Sales Report']
# Add a header format
header_format = workbook.add_format({
'bold': True,
'text_wrap': True,
'valign': 'top',
'fg_color': '#D7E4BC',
'border': 1})
# Write column headers with defined format
for col_num, value in enumerate(df.columns.values):
worksheet.write(0, col_num, value, header_format)
# Add a chart
chart = workbook.add_chart({'type': 'column'})
# Configure the chart from the dataframe data,
# using the first column for x, and second column for values
chart.add_series({'values': '=Sales Report!$D$2:$D$'+str(len(df)+1),
'categories': '=Sales Report!$A$2:$A$'+str(len(df)+1),
'name': "Sales",
})
worksheet.insert_chart('F2', chart)
Step 7: Save and close the Excel writer
writer.save()
In conclusion, the Python libraries pandas and xlsxwriter are powerful tools that can help to automatically generate an Excel report. We can manipulate data with pandas, and then format, organize, and write data to an Excel file with xlsxwriter. For more complex reports, remember that xlsxwriter can be used to add charts, images, and even formulas to Excel files.
AI agent for developers
Boost your productivity with Mate:
easily connect your project, generate code, and debug smarter - all powered by AI.
Do you want to solve problems like this faster? Download now for free.