Skip to content

Instantly share code, notes, and snippets.

@dalmosantos
Created May 7, 2025 22:48
Show Gist options
  • Save dalmosantos/c17eb5e463ae910fab7f549185301ac8 to your computer and use it in GitHub Desktop.
Save dalmosantos/c17eb5e463ae910fab7f549185301ac8 to your computer and use it in GitHub Desktop.
create graph from excel file and save pdf
import pandas as pd
import matplotlib.pyplot as plt
# Read the XLSX file
df = pd.read_excel("employees.xlsx")
# Count the frequency of each Team (ignoring null values)
team_counts = df['Team'].value_counts(dropna=True)
# Calculate the percentage
team_percent = (team_counts / team_counts.sum()) * 100
# Calculate the total number of employees
total_employees = team_counts.sum()
# Plot the bar chart with percentage labels
plt.figure(figsize=(14, 7))
bars = plt.bar(team_percent.index, team_percent.values, color='skyblue')
plt.ylabel('Percentage (%)')
plt.xlabel('Team')
plt.title(f'Percentage distribution of employees by team\nTotal employees: {total_employees}')
# Rotate team names to avoid truncation
plt.xticks(rotation=45, ha='right', fontsize=11)
# Add percentage labels to each bar
for bar, percent in zip(bars, team_percent.values):
plt.text(bar.get_x() + bar.get_width() / 2, bar.get_height() + 1,
f'{percent:.1f}%', ha='center', va='bottom', fontsize=10)
plt.ylim(0, team_percent.max() + 10)
plt.tight_layout()
# Save as PNG
plt.savefig('team_chart.png')
plt.close()
print("Chart saved as team_chart.png")
import pandas as pd
from reportlab.lib.pagesizes import A4
from reportlab.pdfgen import canvas
from reportlab.lib.units import cm
from reportlab.platypus import Table, TableStyle, SimpleDocTemplate, Image, PageBreak
from reportlab.lib import colors
from openpyxl import load_workbook
# Input files
chart_path = "grafico_team.png"
xlsx_path = "employees.xlsx"
pdf_path = "employee_report.pdf"
# Load data from Excel
df = pd.read_excel(xlsx_path)
# Create PDF document
doc = SimpleDocTemplate(
pdf_path,
pagesize=A4,
leftMargin=2*cm,
rightMargin=2*cm,
topMargin=2*cm,
bottomMargin=2*cm
)
elements = []
# Page 1: Chart
img = Image(chart_path, width=16*cm, height=8*cm)
elements.append(img)
elements.append(PageBreak())
# Other pages: Formatted table
# Limit number of rows per page
rows_per_page = 30
data = [df.columns.tolist()] + df.values.tolist()
for i in range(0, len(data)-1, rows_per_page):
table = Table([data[0]] + data[i+1:i+1+rows_per_page], repeatRows=1)
table.setStyle(TableStyle([
('BACKGROUND', (0,0), (-1,0), colors.lightblue),
('TEXTCOLOR', (0,0), (-1,0), colors.white),
('ALIGN', (0,0), (-1,-1), 'CENTER'),
('FONTNAME', (0,0), (-1,0), 'Helvetica-Bold'),
('FONTSIZE', (0,0), (-1,0), 10),
('FONTSIZE', (0,1), (-1,-1), 8),
('BOTTOMPADDING', (0,0), (-1,0), 8),
('GRID', (0,0), (-1,-1), 0.25, colors.grey),
('ROWBACKGROUNDS', (0,1), (-1,-1), [colors.whitesmoke, colors.lightgrey])
]))
elements.append(table)
if i + rows_per_page < len(data)-1:
elements.append(PageBreak())
# Generate PDF
doc.build(elements)
print(f"PDF generated: {pdf_path}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment