0Pricing
Python Academy · Lesson

Writing and Styling Cells

Create and format spreadsheets.

Writing and Styling Cells is a free Python Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Creating Spreadsheets

Beyond reading, openpyxl can build spreadsheets from scratch: write values, format cells, and style them so the output looks professional rather than raw.

This lesson covers writing data and applying formatting.

A New Workbook

Workbook() creates an empty workbook with one sheet. Grab it with ws = wb.active and optionally rename it via ws.title = 'Report'.

You add more sheets with wb.create_sheet('Details').

Writing Values

Assign to a cell directly: ws['A1'] = 'Name' or ws.cell(row=1, column=1, value='Name'). To add a whole row at the bottom, use ws.append([...]).

The append pattern is great for writing tables row by row.

header = ['Name', 'Score']
data = [['Ada', 95], ['Linus', 88], ['Grace', 99]]
print('append:', header)
for row in data:
    print('append:', row)

Saving the File

Nothing is written to disk until you call wb.save('report.xlsx'). Forgetting to save is the most common beginner mistake. Always save after the last write.

Fonts

Styling uses small style objects from openpyxl.styles. A Font(bold=True, size=14, color='FF0000') assigned to cell.font makes the text bold, larger, and red.

Colors are eight-character ARGB hex strings.

alpha = 'FF'
rgb = '0000FF'
argb = alpha + rgb
print('Blue, fully opaque:', argb)

Fills and Background Color

A PatternFill(start_color='FFFF00', fill_type='solid') on cell.fill paints the cell background, for example highlighting a header row in yellow. Fills draw the eye to important areas.

Alignment

Alignment(horizontal='center', vertical='center', wrap_text=True) on cell.alignment centers text and wraps long content within the cell. Good alignment makes tables readable.

Borders

Borders are built from Side objects wrapped in a Border: a thin side on all four edges outlines a cell. Applying the same border to a range creates a clean grid.

Number Formats

Set how numbers display with cell.number_format, for example '#,##0.00' for two decimals with thousands separators, or '0%' for percentages. The stored value stays the same; only the display changes.

value = 1234567.5
formatted = format(value, ',.2f')
print('Stored:', value)
print('Displayed:', formatted)

Column Widths and Merging

Set a column width with ws.column_dimensions['A'].width = 20. Merge cells for a title banner with ws.merge_cells('A1:D1'). These finishing touches make output look hand-crafted.

Reusing Style Objects

Style objects like Font and PatternFill can be created once and applied to many cells. Define a header style, then loop over the header row assigning it to each cell. This keeps formatting consistent and the code short.

Treat styles like reusable constants rather than recreating them per cell.

Quick Check

Test your writing and styling knowledge.

Recap

You can now build styled spreadsheets:

  • Workbook(), wb.active, create_sheet, and ws.append
  • Always finish with wb.save(path)
  • Style with Font, PatternFill, Alignment, and Border from openpyxl.styles
  • number_format for display, plus column widths and merge_cells

Frequently asked questions

Is the “Writing and Styling Cells” lesson free?

Yes — the full text of “Writing and Styling Cells” is free to read here on the web, and the Python Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python Academy course, upgrade to CoddyKit PRO.

What will I learn in “Writing and Styling Cells”?

Create and format spreadsheets. You practise Python Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Python Academy?

No prior experience is required. Python Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Writing and Styling Cells” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Python Academy lesson?

Yes. Every Python Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Reading Workbooks
  2. Writing and Styling Cells
  3. Formulas and Charts
  4. Batch Processing Reports
← Back to Python Academy