Working with Excel - openpyxl Basics
What openpyxl is
Section titled “What openpyxl is”openpyxl lets you work with .xlsx files:
- create workbooks
- read/write cells
- adjust styles
Create a workbook
Section titled “Create a workbook”from openpyxl import Workbook
wb = Workbook()
ws = wb.active
ws.title = "Report"
ws["A1"] = "Name"
ws["B1"] = "Score"
ws.append(["Alice", 95])
ws.append(["Bob", 88])
wb.save("report.xlsx")Read an existing workbook
Section titled “Read an existing workbook”from openpyxl import load_workbook
wb = load_workbook("report.xlsx")
ws = wb["Report"]
print(ws["A2"].value, ws["B2"].value)- Keep a template
.xlsxif you need complex formatting - For CSV, prefer the
csvmodule or pandas
🧪 Try It Yourself
Section titled “🧪 Try It Yourself”Exercise 1 – List Files with os.listdir
Section titled “Exercise 1 – List Files with os.listdir”Exercise 2 – Join Paths with os.path.join
Section titled “Exercise 2 – Join Paths with os.path.join”Exercise 3 – Write and Read a File
Section titled “Exercise 3 – Write and Read a File”pch.coffeeTagline
pch.coffeeCtapch.feedbackHeading
pch.feedbackSubheading