slip / app.py
Admin-002's picture
Create app.py
c0a0088 verified
import pandas as pd
import os
import shutil
import zipfile
import tempfile
import matplotlib.pyplot as plt
from matplotlib.patches import FancyBboxPatch, Circle, Rectangle
import gradio as gr
def plot_mobile_bank_slip_from_excel(excel_file):
# Load data from the Excel file
df = pd.read_excel(excel_file)
# Create a temporary directory to store images
temp_dir = tempfile.mkdtemp()
# Iterate over each record and generate the image
for index, row in df.iterrows():
status = row['Status']
reference_no = row['reference_no']
total_amount = row['total_amount']
payee_name = row['payee_name']
to_account_no = row['to_account_no']
# Create a figure and axis
fig, ax = plt.subplots(figsize=(6, 10))
# Draw sections with curved corners
successful_patch = FancyBboxPatch((0, 0.85), 1, 0.1, boxstyle="round,pad=0.1", ec="none", fc='green' if status == "Successful" else 'yellow' if status == "Pending" else 'Red', alpha=0.5)
transaction_patch = FancyBboxPatch((0, 0.4), 1, 0.1, boxstyle="round,pad=0.1", ec="none", fc='lightblue', alpha=0.5)
ax.add_patch(successful_patch)
ax.add_patch(transaction_patch)
# Draw title
ax.text(0.5, 0.9, "Transaction Status", color='navy', fontsize=24, ha='center')
# Draw payment status
ax.text(0.5, 0.85, f" {status}", color='black', fontsize=16, ha='center')
# Draw transaction ID
ax.text(0.5, 0.8, f"Reference no: {reference_no}", color='black', fontsize=12, ha='center')
# Draw total amount
ax.text(0.5, 0.71, "Total Amount Transferred", color='black', fontsize=12, ha='center')
ax.text(0.5, 0.67, f"₹{total_amount}", color='black', fontsize=20, ha='center')
# Extract first letter from first name and last name
first_name, last_name = payee_name.split() if payee_name else ("?", "?")
first_initial, last_initial = first_name[0].upper(), last_name[0].upper()
# Draw circular profile with initials
profile_circle = Circle((0.5, 0.6), 0.04, color='#05cdf5')
ax.add_patch(profile_circle)
ax.text(0.5, 0.6, f"{first_initial}{last_initial}", color='black', fontsize=12, ha='center', va='center')
# Draw payee details and values
details = {"Payee": payee_name, "To Account No": to_account_no}
max_len = max(len(detail) for detail in details.keys())
for i, (detail, value) in enumerate(details.items()):
ax.text(0.1, 0.45 - i * 0.06, f"{detail.ljust(max_len)}", color='black', fontsize=14, ha='left')
ax.text(0.9, 0.45 - i * 0.06, f"{value}", color='black', fontsize=14, ha='right')
# Draw buttons with symbols
button_width = 0.50
button_height = 0.05
button_x_share = 0.2 - button_width / 2
button_x_download = 0.75 - button_width / 2
ax.add_patch(Rectangle((button_x_share, 0.15 - button_height / 2), button_width, button_height, edgecolor='none', facecolor='#f4a460'))
ax.add_patch(Rectangle((button_x_download, 0.15 - button_height / 2), button_width, button_height, edgecolor='none', facecolor='#34c3eb'))
ax.text(0.225, 0.15, "Share Receipt", color='white', fontsize=14, ha='center', va='center')
ax.text(0.75, 0.15, "Download Receipt", color='white', fontsize=14, ha='center', va='center')
# Set axis limits and remove ticks
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
ax.axis('off')
# Save the image
image_filename = f"{temp_dir}/transaction_{index + 1}.png"
plt.savefig(image_filename, bbox_inches='tight', pad_inches=0.1)
plt.close()
# Create a zip file containing the images
shutil.make_archive(temp_dir, 'zip', temp_dir)
zip_file_path = f"{temp_dir}.zip"
return zip_file_path
# Create the Gradio interface
inputs = gr.File(label="Upload Excel File")
outputs = gr.File(label="Download Output Images")
gr.Interface(fn=plot_mobile_bank_slip_from_excel, inputs=inputs, outputs=outputs).launch()