When you're dealing with invoices from multiple clients, you quickly realize that PDFs, Excel files, and HTML tables are a nightmare to parse manually. I recently had to migrate a legacy accounting system to a modern one, and the only format the new system accepted was CSV. Here's a quick Python script I used to convert a batch of invoices from various formats into clean CSV files, plus a handy web tool for when you need a no-code solution.
python
import pandas as pd
import pdfplumber
from pathlib import Path
Convert PDF to CSV
def pdf_to_csv(pdf_path):
with pdfplumber.open(pdf_path) as pdf:
rows = []
for page in pdf.pages:
table = page.extract_table()
if table:
rows.extend(table)
df = pd.DataFrame(rows[1:], columns=rows[0])
return df
Convert Excel to CSV
def excel_to_csv(xlsx_path):
return pd.read_excel(xlsx_path, engine='openpyxl')
Convert HTML table to CSV
def html_to_csv(html_path):
return pd.read_html(html_path)[0]
Batch process all invoices in a folder
folder = Path('./invoices')
for file in folder.glob('.'):
suffix = file.suffix.lower()
if suffix == '.pdf':
df = pdf_to_csv(file)
elif suffix in ['.xls', '.xlsx']:
df = excel_to_csv(file)
elif suffix == '.html':
df = html_to_csv(file)
else:
continue
output_path = folder / f"{file.stem}.csv"
df.to_csv(output_path, index=False)
print(f"Converted {file.name} -> {output_path.name}")
This script handles the basics, but if you're in a hurry or dealing with complex invoice layouts, check out SERPSpur's Invoice to CSV Converter—it supports PDF, XLS, XLSX, and HTML, and gives you import-ready CSVs instantly without writing a single line of code.
Top comments (3)
Great approach — I've been in a similar migration mess and found that PDFs with rotated text or images embedded in tables can trip up pdfplumber. Does your web tool use OCR or just table extraction for those edge cases?
This script is a solid starting point, but I'd add error handling for cases like password-protected PDFs or corrupted Excel files—those always trip me up. The web tool sounds handy for quick jobs, though; I'll bookmark it for next time I'm buried in invoice conversions.
This is a solid script — I've used pdfplumber before and it's great for simple tables. One thing to watch out for is PDFs with merged cells or multi-line headers; those can break the extraction. Have you run into that, and does your tool handle them better?