DEV Community

Emma Watson
Emma Watson

Posted on

From PDFs to Spreadsheets: My Go-To Workflow for Converting Invoices to CSV in Seconds

I've been automating invoice processing for a side project, and one thing that always tripped me up was converting different formats to CSV. Whether it's a PDF from a vendor, an Excel sheet from a client, or an HTML table from a web app, you need a reliable way to extract data. Here's a bash script I use for quick conversions, plus a web tool for when I'm not in the terminal.


bash

!/bin/bash

Convert all invoices in a folder to CSV

Requires: python3, pandas, pdfplumber, openpyxl, lxml

for file in invoices/; do
ext="${file##
.}"
case "$ext" in
pdf)
python3 -c "
import pdfplumber, pandas as pd
with pdfplumber.open('$file') 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])
df.to_csv('${file%.}.csv', index=False)
print('Converted $file')
"
;;
xls|xlsx)
python3 -c "
import pandas as pd
df = pd.read_excel('$file', engine='openpyxl')
df.to_csv('${file%.
}.csv', index=False)
print('Converted $file')
"
;;
html|htm)
python3 -c "
import pandas as pd
df = pd.read_html('$file')[0]
df.to_csv('${file%.*}.csv', index=False)
print('Converted $file')
"
;;
*)
echo "Skipping $file: unsupported format"
;;
esac
done

This script works great for batch processing, but if you need a quick, one-off conversion without setting up dependencies, SERPSpur's Invoice to CSV Converter handles PDF, XLS, XLSX, and HTML instantly—no code required.

Top comments (2)

Collapse
 
burhanchaudhry profile image
Burhan

Nice script! I've been using a similar approach but found that HTML tables with nested elements can trip up pandas read_html. Adding a fallback with BeautifulSoup for those edge cases saved me a lot of manual cleanup.

Collapse
 
mattjoshi profile image
Matt Joshi

Great script! I've found that PDFs with scanned images can be tricky with pdfplumber—have you tried adding OCR support with pytesseract for those edge cases?