Untitled

mail@pastecode.io avatar
unknown
plain_text
16 days ago
1.2 kB
1
Indexable
Never
from openpyxl import Workbook
from openpyxl.chart import BarChart, Reference

def krijo_excel_me_grafik(filepath):
    try:
        wb = Workbook()
        ws = wb.active
        headers = ['Emri', 'Mosha', 'Perqindja']
        ws.append(headers)

        data = [
            ["Artan", 30, 40],
            ["Besa", 40, 15],
            ["Ilir", 35, 25],
            ["Andi", 28, 20]
        ]

        for row in data:
            ws.append(row)

        chart = BarChart()
        chart.title = "Grafik perfaqesues"
        chart.x_axis.title = "Emri"
        chart.y_axis.title = "Perqindja e perfaqesimit ne kompani"
        chart.style = 10

        data_ref = Reference(ws, min_col=3, min_row=1, max_row=len(data) + 1, max_col=3)
        categories_ref = Reference(ws, min_col=1, min_row=2, max_row=len(data) + 1, max_col=1)

        chart.add_data(data_ref, titles_from_data=True)
        chart.set_categories(categories_ref)

        ws.add_chart(chart, "E5")
        wb.save(filepath)
        print("Faqja në Excel u gjenerua me sukses.")

    except Exception as e:
        print(f"Gabim gjatë krijimit të faqesës së Excel: {e}")

filepath = "grafiku.xlsx"
krijo_excel_me_grafik(filepath)
Leave a Comment