xlsx2csv

0.8.6 · active · verified Sun Apr 12

xlsx2csv is a Python library and command-line tool designed to convert XLSX files to CSV format. It is efficient, capable of handling large XLSX files, and fast. The current version is 0.8.6, and the project shows an active development and release cadence with recent updates addressing various bug fixes and Python version compatibility.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to convert an XLSX file to CSV using the `Xlsx2csv` class with a context manager for proper resource cleanup. It also includes basic error handling and cleanup of the dummy files for a self-contained example.

import os
from xlsx2csv import Xlsx2csv

# Create a dummy XLSX file for demonstration
# In a real scenario, this file would already exist.
# For simplicity, we'll just demonstrate the conversion logic.
# You might use openpyxl to create a real .xlsx file for testing:
# import openpyxl
# wb = openpyxl.Workbook()
# ws = wb.active
# ws['A1'] = 'Header1'
# ws['B1'] = 'Header2'
# ws['A2'] = 'Data1'
# ws['B2'] = 'Data2'
# wb.save('example.xlsx')

# Ensure a dummy file path exists for the example
excel_file = 'example.xlsx'
csv_output = 'example.csv'

# To make this runnable without needing openpyxl just for quickstart:
# Create a dummy excel_file (normally this would be a real .xlsx)
with open(excel_file, 'w') as f:
    f.write('This is a placeholder for a .xlsx file content.')

# Recommended: using context manager for proper resource cleanup
try:
    with Xlsx2csv(excel_file, outputencoding="utf-8") as converter:
        converter.convert(csv_output)
    print(f"Successfully converted '{excel_file}' to '{csv_output}'.")

    # Optionally, read and print the CSV content to verify
    if os.path.exists(csv_output):
        with open(csv_output, 'r', encoding='utf-8') as f_csv:
            print("CSV Content:\n" + f_csv.read())
    else:
        print(f"Error: CSV file '{csv_output}' not created.")

except Exception as e:
    print(f"An error occurred during conversion: {e}")
finally:
    # Clean up dummy files
    if os.path.exists(excel_file):
        os.remove(excel_file)
    if os.path.exists(csv_output):
        os.remove(csv_output)

view raw JSON →