समझिए
लेसन 3 की report.xlsx फ़ॉर्मेट करेंगे।
1. खोलें, चुनें, सेव करें
from openpyxl import load_workbook
wb = load_workbook("report.xlsx")
ws = wb["Summary"] # or wb.active
# ... changes ...
wb.save("report.xlsx")
सेल: ws["A1"], ws.cell(row=2, column=3)। पूरी रो 1: ws[1]। आकार: ws.max_row, ws.max_column।
2. हेडर स्टाइल और बॉर्डर
from openpyxl.styles import Font, PatternFill, Alignment, Border, Side
header_fill = PatternFill("solid", start_color="1F4E78")
thin = Side(style="thin", color="BFBFBF")
for cell in ws[1]:
cell.font = Font(bold=True, color="FFFFFF")
cell.fill = header_fill
cell.alignment = Alignment(horizontal="center")
for row in ws.iter_rows(min_row=1, max_row=ws.max_row, max_col=ws.max_column):
for cell in row:
cell.border = Border(top=thin, bottom=thin, left=thin, right=thin)
रंग बिना # के hex कोड होते हैं।
3. नंबर फ़ॉर्मेट
for row in ws.iter_rows(min_row=2, min_col=2, max_col=2): # column B, from row 2
for cell in row:
cell.number_format = "#,##0"
दूसरे फ़ॉर्मेट: "0.00%", "dd-mmm-yyyy", '"₹"#,##0'।
4. कॉलम चौड़ाई (AutoFit जैसी)
openpyxl में AutoFit नहीं है, इसलिए सबसे लंबी वैल्यू नापें:
from openpyxl.utils import get_column_letter
for col_cells in ws.columns:
longest = max(len(str(c.value)) if c.value is not None else 0 for c in col_cells)
ws.column_dimensions[get_column_letter(col_cells[0].column)].width = longest + 3
5. Freeze panes और फ़िल्टर
ws.freeze_panes = "A2" # keep row 1 visible while scrolling
ws.auto_filter.ref = ws.dimensions # filter arrows on the whole table, e.g. "A1:B4"
6. Conditional formatting
70,000 से कम टोटल को लाल करें:
from openpyxl.formatting.rule import CellIsRule, ColorScaleRule
last = ws.max_row
ws.conditional_formatting.add(
f"B2:B{last}",
CellIsRule(operator="lessThan", formula=["70000"],
fill=PatternFill("solid", start_color="FFC7CE"), font=Font(color="9C0006")))
Raw data की रकम पर 3-रंग स्केल (लाल → पीला → हरा):
raw = wb["Raw Data"]
raw.conditional_formatting.add(
f"E2:E{raw.max_row}",
ColorScaleRule(start_type="min", start_color="F8696B", mid_type="percentile", mid_value=50,
mid_color="FFEB84", end_type="max", end_color="63BE7B"))
ये असली Excel नियम हैं — कोई नंबर बदले तो भी काम करते रहते हैं।
7. चार्ट
from openpyxl.chart import BarChart, Reference
chart = BarChart()
chart.title = "Sales by Region"
chart.y_axis.title = "Amount (₹)"
data = Reference(ws, min_col=2, min_row=1, max_row=last) # includes header for series name
cats = Reference(ws, min_col=1, min_row=2, max_row=last)
chart.add_data(data, titles_from_data=True)
chart.set_categories(cats)
chart.width, chart.height = 16, 8
ws.add_chart(chart, "E2")
और भी उपलब्ध: LineChart, PieChart, ScatterChart। चार्ट असली Excel चार्ट है, यूज़र उसे एडिट कर सकता है।
8. फ़ॉर्मूले
ws[f"A{last + 1}"] = "Total"
ws[f"B{last + 1}"] = f"=SUM(B2:B{last})"
ws[f"B{last + 1}"].font = Font(bold=True)
फ़ाइल खुलने पर Excel फ़ॉर्मूले की गणना करता है।
9. इसे फ़ंक्शन बनाएँ
फ़ॉर्मेटिंग को एक फ़ंक्शन में रखें जिसे हर शीट के लिए बुलाएँ — लेसन 6 का मिनी प्रोजेक्ट बिल्कुल यही करता है।
आम गलतियाँ
wb.save() भूलना। 1F4E78 की जगह #1F4E78 लिखना। ws.max_row की जगह B2:B4 जैसी रेंज फ़िक्स लिखना। स्क्रिप्ट सेव करते समय फ़ाइल Excel में खुली रखना।