Skip to main content
🛡️ Verified Technical Content: Written by Serhii Hrekov. | Last reviewed & updated in Git: July 21, 2026

Python Barcode Processing: Export Data, Generate 2D Codes, and Database Integration

· 6 min read
Serhii Hrekov
Senior Software Engineer & System Architect specializing in Python, Web Systems, Cloud Infrastructure & Automation
  • tooling image: /img/blog/thumbnails/exporting-python-barcode-scan-data-csv-excel.png

Exporting your scan data is the final piece of the puzzle. While a database is great for storage, most team members prefer to see results in a spreadsheet.

In Python, the pandas library is the gold standard for this. It can read directly from your SQLite database and convert that data into a professional-looking Excel or CSV file in just a few lines of code.

Exporting Scan Data to CSV and Excel

1. Installation

To handle Excel files (.xlsx), pandas needs a helper library called openpyxl.

pip install pandas openpyxl

2. The Export Script

This function connects to your inventory.db, pulls all the recorded scans, and saves them to your choice of format.

import sqlite3
import pandas as pd

def export_scans(format="csv"):
# 1. Connect to the database
conn = sqlite3.connect("inventory.db")

# 2. Use Pandas to read the table directly into a DataFrame
# A DataFrame is like a virtual spreadsheet in memory
df = pd.read_sql_query("SELECT * FROM scans", conn)

# 3. Export based on the requested format
if format.lower() == "csv":
df.to_csv("scan_report.csv", index=False)
print("✅ Report exported to scan_report.csv")
elif format.lower() == "excel":
df.to_excel("scan_report.xlsx", index=False, engine='openpyxl')
print("✅ Report exported to scan_report.xlsx")

conn.close()

# export_scans("excel")


3. Adding Value: Auto-Export on Exit

You can modify your scanner script from the previous article to automatically generate a report whenever the user quits (presses 'q'). This ensures the "Manager's Report" is always up-to-date at the end of a shift.

# Inside your main live_scanner_with_db() function:
# ... after the while loop breaks ...

print("Shutting down scanner...")
cap.release()
conn.close()
cv2.destroyAllWindows()

# Trigger the auto-export
print("Generating end-of-session report...")
export_scans("excel")


4. Why Use Pandas Over the Built-in csv Module?

While Python has a built-in csv module, using pandas offers several advantages for production environments:

  • Header Handling: It automatically uses your database column names as the spreadsheet headers.
  • Data Cleaning: If you have malformed data or empty timestamps, pandas can fix them in one line before exporting.
  • Multiple Formats: One library handles CSV, Excel, JSON, and even HTML tables.
  • Performance: It is highly optimized for large datasets (e.g., thousands of inventory items).

Sources


Generating Data Matrix and PDF417 Codes in Python

Beyond standard 1D barcodes, Python can generate advanced 2D barcode formats used in industrial applications:

Data Matrix

Data Matrix is a 2D barcode standard (ISO/IEC 16022) commonly used in electronics manufacturing, healthcare, and logistics. It can store up to 2,335 alphanumeric characters in a compact square format.

PDF417

PDF417 is a stacked linear barcode format (ISO/IEC 15438) used in government-issued IDs, shipping labels, and airline boarding passes. It can store up to 1,850 alphanumeric characters.

Both formats can be generated using the treepoem library, which wraps the BWIPP (Barcode Writer in Pure PostScript) engine and supports 100+ barcode formats.

pip install treepoem

Integrating Barcode Scanners with a Database

For persistent barcode data, integrating with SQLite provides:

  • Persistence: Data survives crashes and restarts.
  • Querying: Run SQL commands to analyze scan patterns.
  • Scalability: The code structure is 90% identical for PostgreSQL or MySQL.

The typical workflow: decode barcodes using pyzbar, validate with a deduplication set, then store in a SQLite database with timestamps for analysis.


Additional Sources