0% found this document useful (0 votes)
0 views4 pages

Code Python

The document provides Python code that reads an Excel file to calculate total profit per product and generates a pie chart to visualize each product's contribution to total profit. It includes steps for handling negative profits, saving the pie chart as an image, and generating a PDF report with a summary table and the pie chart. The code utilizes libraries such as pandas and matplotlib for data manipulation and visualization, and reportlab for PDF creation.

Uploaded by

hemangi.adaca
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views4 pages

Code Python

The document provides Python code that reads an Excel file to calculate total profit per product and generates a pie chart to visualize each product's contribution to total profit. It includes steps for handling negative profits, saving the pie chart as an image, and generating a PDF report with a summary table and the pie chart. The code utilizes libraries such as pandas and matplotlib for data manipulation and visualization, and reportlab for PDF creation.

Uploaded by

hemangi.adaca
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Sure — here’s the complete Python code in plain text format that reads your Excel file, calculates

total profit per product, and plots a pie chart showing each product’s contribution to total profit:

import pandas as pd

import [Link] as plt

# Step 1: Load the Excel file

file_path = "Financial [Link]" # Change path if needed

df = pd.read_excel(file_path)

# Step 2: Calculate profit per unit

df["Profit per Unit"] = df["Sale Price"] - df["Manufacturing Price"]

# Step 3: Calculate total profit per product

product_profit = [Link]("Product").apply(

lambda x: (x["Profit per Unit"] * x["Units Sold"]).sum()

).reset_index(name="Total Profit")

# Step 4: Handle negative or zero profits for pie chart

product_profit["Total Profit"] = product_profit["Total Profit"].clip(lower=0)

# Step 5: Display total profit per product

print(product_profit)

# Step 6: Plot pie chart showing contribution of each product

[Link](figsize=(8, 8))

[Link](product_profit["Total Profit"],

labels=product_profit["Product"],

autopct="%1.1f%%",

startangle=140)

[Link]("Total Profit Contribution by Product")


[Link]()

Would you like me to modify this so it saves the pie chart as an image or writes the profit summary
back into a new Excel file?

import pandas as pd

import [Link] as plt

from [Link] import A4

from [Link] import SimpleDocTemplate, Paragraph, Spacer, Table, TableStyle, Image

from [Link] import colors

from [Link] import getSampleStyleSheet

# === Step 1: Load Excel file ===

file_path = "Financial [Link]" # Change path if needed

df = pd.read_excel(file_path)

# === Step 2: Calculate profit per unit and total profit per product ===

df["Profit per Unit"] = df["Sale Price"] - df["Manufacturing Price"]

product_profit = [Link]("Product").apply(

lambda x: (x["Profit per Unit"] * x["Units Sold"]).sum()

).reset_index(name="Total Profit")

# Replace any negative profits with 0 (for pie chart display)

product_profit["Total Profit"] = product_profit["Total Profit"].clip(lower=0)

# === Step 3: Create pie chart ===

chart_path = "profit_pie_chart.png"

[Link](figsize=(8, 8))

[Link](product_profit["Total Profit"], labels=product_profit["Product"], autopct="%1.1f%%",


startangle=140)

[Link]("Total Profit Contribution by Product")


[Link](chart_path)

[Link]()

# === Step 4: Generate PDF report ===

pdf_path = "Product_Profit_Report.pdf"

doc = SimpleDocTemplate(pdf_path, pagesize=A4)

styles = getSampleStyleSheet()

elements = []

# Title

[Link](Paragraph("<b>Product Profit Analysis Report</b>", styles["Title"]))

[Link](Spacer(1, 12))

# Summary Table

table_data = [["Product", "Total Profit (₹)"]] + product_profit.[Link]()

table = Table(table_data, hAlign='LEFT')

[Link](TableStyle([

('BACKGROUND', (0, 0), (-1, 0), [Link]),

('TEXTCOLOR', (0, 0), (-1, 0), [Link]),

('ALIGN', (0, 0), (-1, -1), 'CENTER'),

('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),

('BOTTOMPADDING', (0, 0), (-1, 0), 12),

('BACKGROUND', (0, 1), (-1, -1), [Link]),

('GRID', (0, 0), (-1, -1), 1, [Link]),

]))

[Link](table)

[Link](Spacer(1, 20))

# Chart

[Link](Paragraph("<b>Pie Chart - Total Profit Contribution by Product</b>",


styles["Heading2"]))
[Link](Image(chart_path, width=400, height=400))

# Build PDF

[Link](elements)

print(f"✅ PDF report successfully created: {pdf_path}")

You might also like