Stop 11 · Automation & ScriptingTopics · 6/7

Automating Excel & PDF Files

Part of the Python Roadmap

In short

Every office runs on spreadsheets and PDFs, and someone spends hours each month copying numbers between them. With openpyxl and pypdf, a script builds the monthly report and merges the invoices while you have your chai.

openpyxl reads and writes Excel workbooks, including formulas and formatting, and libraries like pypdf can merge, split and extract text from PDFs. Together they automate a lot of office paperwork.

The jaadu sits at a busy office desk, filling a large green ledger with one hand while stapling a thick bundle of papers together with the other.

Think of it like this

The tireless office clerk

Picture a clerk in a Kanpur office who never gets bored: fills the monthly register in neat columns, totals the last row, then staples twelve invoices into one bundle in the right order. openpyxl fills the register, and pypdf does the stapling.

Watch it happen

Month-end, automated

Sales data
Workbook
Formula
Merge PDFs

1/4The script starts with this month's sales by city, from a list, CSV or database.

The big ideas

What you need to know

openpyxl for .xlsx

Workbook() makes a new file, load_workbook() opens one. ws.append(row) adds rows, ws["B5"] reads or writes a cell.

Formulas are text

openpyxl writes formulas but doesn't calculate them. Excel or LibreOffice computes them when the file opens.

pypdf for PDFs

PdfReader reads pages and extracts text, and PdfWriter builds new files from merged, split or rotated pages.

pandas for big tables

For heavy analysis, read with pandas.read_excel and write back with to_excel. openpyxl is best for layout and formatting.

See it in code

Walk through the code

python
1from openpyxl import Workbook2from openpyxl.styles import Font3from pypdf import PdfReader, PdfWriter4 5sales = [("Pune", 182000), ("Nagpur", 96500), ("Nashik", 74250)]6 7wb = Workbook()8ws = wb.active9ws.title = "November"10ws.append(["City", "Revenue (Rs)"])11for row in sales:12    ws.append(row)13ws.append(["Total", "=SUM(B2:B4)"])14for cell in ws[1]:15    cell.font = Font(bold=True)16wb.save("sales_report.xlsx")17 18writer = PdfWriter()19for name in ["invoice_sep.pdf", "invoice_oct.pdf", "invoice_nov.pdf"]:20    for page in PdfReader(name).pages:21        writer.add_page(page)22with open("invoices_q3.pdf", "wb") as f:23    writer.write(f)

1/4A new workbook has one sheet, wb.active. Rename it to something useful.

pip install openpyxl pypdf. Put the three invoice PDFs next to the script before running.

Save yourself the pain

Common mistakes

Oops

Expecting openpyxl to give you the result of a formula you just wrote.

Do this instead

It stores the formula only. Calculate totals in Python if your script needs the number.

Oops

Saving over the original workbook while testing.

Do this instead

Save to a new file name until you trust the script. A bad save can wipe formatting or data.

Oops

Expecting extract_text() to work on scanned PDFs.

Do this instead

Scanned pages are images with no text layer. You need OCR for those.

Your mission

Automate a fee register

~35 min

Quick check

You write ws["B5"] = "=SUM(B2:B4)" and save. Where does the actual total get calculated?

Go deeper

  1. 1Create an Excel report from a list of dicts with openpyxl.
  2. 2Merge several PDF invoices into one file with pypdf.
  3. 3Read an Excel sheet, total a column and write the result to a new sheet.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge