, ,

Python Automation – Excel Charts Creation with Threshold / Target Range / Bands in the Background

Python automation for creating Excel charts with threshold, target range, or bands in the background involves using Python code to generate charts in Excel that visually represent data. These charts can help in analyzing trends, setting goals, or monitoring progress.

In this process, Python libraries like openpyxl and xlwings are often used to interact with Excel files. Data is first prepared or imported into an Excel sheet using Python, and then a chart is created based on this data. The chart is customized to include a threshold, target range, or bands in the background, which can be used to highlight specific values or goals.

This automation is useful in various scenarios, such as sales tracking, performance monitoring, or project management, where visual representations of data help in better understanding and decision-making. By automating this process, repetitive tasks can be eliminated, and charts can be generated quickly and consistently.

Let us get into the code know. Create a python file with .py extension and add the below code to it , you can also take your own excel sheet to perform the same.

from openpyxl import Workbook
from openpyxl.chart import LineChart, Reference

# Create a new Excel workbook
wb = Workbook()
ws = wb.active

# Insert data into the Excel sheet
data = [
['Month', 'Sales'],
['Jan', 100],
['Feb', 120],
['Mar', 130],
['Apr', 110],
['May', 105],
['Jun', 115],
]
for row in data:
ws.append(row)

# Create a line chart
chart = LineChart()
chart.title = "Sales Trend"
chart.y_axis.title = "Sales"
chart.x_axis.title = "Month"

# Add data to the chart
data = Reference(ws, min_col=2, min_row=2, max_col=2, max_row=7)
categories = Reference(ws, min_col=1, min_row=2, max_row=7)
chart.add_data(data, titles_from_data=True)
chart.set_categories(categories)

# Add a threshold or target range
threshold = 120
chart.threshold = threshold

# Add the chart to the worksheet
ws.add_chart(chart, "E5")

# Save the workbook
wb.save('chart_with_threshold.xlsx')

Output:

Explanation of the above code & how to run the code:

Explanation:

  1. from openpyxl import Workbook: Import the Workbook class from the openpyxl module, which allows us to create Excel files.
  2. from openpyxl.chart import LineChart, Reference: Import the LineChart and Reference classes from the openpyxl.chart module, which are used to create charts in Excel.
  3. wb = Workbook(): Create a new Excel workbook and assign it to the variable wb.
  4. ws = wb.active: Get the active sheet (the default sheet) from the workbook and assign it to the variable ws.
  5. data = [...]: Define a list of lists containing the data to be inserted into the Excel sheet, including months and sales numbers.
  6. for row in data: ws.append(row): Iterate over the data list and append each row to the Excel sheet ws.
  7. chart = LineChart(): Create a new line chart object and assign it to the variable chart.
  8. chart.title = "Sales Trend": Set the title of the chart to “Sales Trend”.
  9. chart.y_axis.title = "Sales": Set the title of the y-axis (vertical axis) to “Sales”.
  10. chart.x_axis.title = "Month": Set the title of the x-axis (horizontal axis) to “Month”.
  11. data = Reference(ws, min_col=2, min_row=2, max_col=2, max_row=7): Create a reference to the sales data in the Excel sheet.
  12. categories = Reference(ws, min_col=1, min_row=2, max_row=7): Create a reference to the month categories in the Excel sheet.
  13. chart.add_data(data, titles_from_data=True): Add the sales data to the chart, with titles automatically taken from the data.
  14. chart.set_categories(categories): Set the categories (months) for the chart.
  15. threshold = 120: Define a threshold value (e.g., a target sales value).
  16. chart.threshold = threshold: Set the threshold value on the chart to visually represent the target range.
  17. ws.add_chart(chart, "E5"): Add the chart to the Excel sheet at cell “E5”.
  18. wb.save('chart_with_threshold.xlsx'): Save the workbook with the charts and data to a file named 'chart_with_threshold.xlsx'.

How to run the code:

  1. Run the Python script that creates the Excel file with the chart.
  2. Open the 'chart_with_threshold.xlsx' file that was created in the same directory as your Python script.
  3. In the Excel file, you will see a line chart titled “Sales Trend” with the sales data plotted against the months.
  4. The threshold line will be visible on the chart, indicating the threshold value of 120. This line represents the target value or threshold for the sales data.
  5. Depending on the type of threshold or band you want to display, you can adjust the chart properties in the Python code. For example, you can change the color or style of the threshold line to make it more visible.

In conclusion, Python automation for creating Excel charts with threshold, target range, or bands in the background offers a powerful way to visualize data and gain insights. By using Python libraries like openpyxl and xlwings, users can automate the process of generating charts, making it faster and more efficient. These charts can be used in various fields, such as sales, finance, and project management, to track progress and set goals. Overall, Python automation simplifies the task of creating complex charts, allowing users to focus more on analyzing the data and making informed decisions.

Don’t forget to share it helped you ! and comment down if have any questions.

Author

Sona Avatar

Written by

Leave a Reply

Trending

CodeMagnet

Your Magnetic Resource, For Coding Brilliance

Programming Languages

Web Development

Data Science and Visualization

Career Section

<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-4205364944170772"
     crossorigin="anonymous"></script>