Install xlsx2csv
masterYou can install xlsx2csv using pip or easy_install. It also works as a standalone script using the xlsx2csv.py file.
Supported Python versions include 2.4, 2.7, and 3.4 through 3.14.
pip install xlsx2csvrepository·master·Indexed 23 days ago
https://github.com/dilshod/xlsx2csvA fast and efficient tool to convert XLSX files to CSV format, optimized for large files using an Expat SAX parser for XML processing. It provides both a command-line interface and a Python programmatic interface via the Xlsx2csv class. Features include support for specific sheet selection, custom date/time/float formatting, delimiter and quoting control, and the ability to handle hidden sheets or rows. Compatible with Python versions 2.4, 2.7, and 3.4 through 3.14.
You can install xlsx2csv using pip or easy_install. It also works as a standalone script using the xlsx2csv.py file.
Supported Python versions include 2.4, 2.7, and 3.4 through 3.14.
pip install xlsx2csvThe xlsx2csv command-line tool converts Excel (.xlsx) files to CSV format. You can specify an input file and an optional output file. If no output file is provided, the result is sent to STDOUT. If the input is a directory, the tool will recursively convert all .xlsx files found within it.
Basic Usage:
xlsx2csv input.xlsx output.csvConvert from STDIN:
Use - as the input file path to read from standard input.
cat input.xlsx | xlsx2csv - output.csvYou can import the Xlsx2csv class to perform conversions programmatically.
Recommended approach: Use a context manager to ensure proper resource cleanup.
Simple approach: Calling .convert() directly on the instance (may cause ResourceWarning in modern Python versions).
from xlsx2csv import Xlsx2csv
# Recommended: using context manager for proper resource cleanup
with Xlsx2csv("myfile.xlsx", outputencoding="utf-8") as xlsx2csv:
xlsx2csv.convert("myfile.csv")
# Simple usage (but may cause ResourceWarning in modern Python)
Xlsx2csv("myfile.xlsx", outputencoding="utf-8").convert("myfile.csv")When initializing Xlsx2csv(xlsxfile, **options), you can pass several options to control the conversion process.
| Option | Default | Description |
|---|---|---|
delimiter | , | CSV column delimiter symbol |
quoting | csv.QUOTE_MINIMAL | How and if to quote fields |
sheetdelimiter | -------- | Delimiter used when processing all sheets |
dateformat | None | Override date/time format |
timeformat | None | Override time format |
floatformat | None | Override float format |
scifloat | False | Whether to support scientific notation for floats |
skip_empty_lines | False | Skip empty lines |
skip_trailing_columns | False | Skip trailing columns |
hyperlinks | False | Include hyperlinks in output |
include_sheet_pattern | ["^.*$"] | List of regex patterns; only sheets matching these are included |
exclude_sheet_pattern | [] | List of regex patterns; sheets matching these are excluded |
exclude_hidden_sheets | False | If True, skip sheets marked as hidden |
skip_hidden_rows | True | If True, skip rows marked as hidden |
outputencoding | "utf-8" | Encoding for the output file |
The following arguments are available for the xlsx2csv.py command line interface:
| Argument | Description |
|---|---|
-a, --all | Export all sheets |
-c OUTPUTENCODING, --outputencoding OUTPUTENCODING | Encoding of output CSV Python 3 only (default: utf-8) |
-s SHEETID, --sheet SHEETID | Sheet number to convert, 0 for all |
-n SHEETNAME, --sheetname SHEETNAME | Sheet name to convert |
-d DELIMITER, --delimiter DELIMITER | Column delimiter; use 'tab' or 'x09' for a tab (default: ,) |
-l LINETERMINATOR, --lineterminator LINETERMINATOR | Line terminator; \n, \r\n, or \r (default: os.linesep) |
-f DATEFORMAT, --dateformat DATEFORMAT | Override date/time format (ex. %Y/%m/%d) |
--floatformat FLOATFORMAT | Override float format (ex. %.15f) |
-i, --ignoreempty | Skip empty lines |
-e, --escape | Escape \r\n\t characters |
-p SHEETDELIMITER, --sheetdelimiter SHEETDELIMITER | Sheet delimiter used to separate sheets (default: '--------') |
-q QUOTING, --quoting QUOTING | Field quoting: 'none', 'minimal', 'nonnumeric', or 'all' (default: 'minimal') |
--hyperlinks | Include hyperlinks |
-I INCLUDE_SHEET_PATTERN, --include_sheet_pattern | Only include sheets matching the pattern (affects -a) |
-E EXCLUDE_SHEET_PATTERN, --exclude_sheet_pattern | Exclude sheets matching the pattern (affects -a) |
-m, --merge-cells | Merge cells |
xlsx2csv.py [-h] [-v] [-a] [-c OUTPUTENCODING] [-s SHEETID]
[-n SHEETNAME] [-d DELIMITER] [-l LINETERMINATOR]
[-f DATEFORMAT] [--floatformat FLOATFORMAT]
[-i] [-e] [-p SHEETDELIMITER]
[--hyperlinks]
[-I INCLUDE_SHEET_PATTERN [INCLUDE_SHEET_PATTERN ...]]
[-E EXCLUDE_SHEET_PATTERN [EXCLUDE_SHEET_PATTERN ...]] [-m]
xlsxfile [outfile]The CLI tool converts XLSX files to CSV format. You can specify a single file or a directory of files.
Basic Syntax:
xlsx2csv.py [options] xlsxfile [outfile]
Directory Conversion:
If you provide an input directory and an output directory, the tool will convert every .xlsx file in the input directory and save the resulting .csv files in the output directory.
python xlsx2csv.py /path/to/input/dir /path/to/output/dirThe Xlsx2csv class provides a programmatic interface to convert XLSX files to CSV. It supports both context manager usage (recommended for proper resource cleanup) and simple direct usage.
Key features:
The convert(outfile, sheetid=1, sheetname=None) method performs the actual conversion.
outfile: A path to the output file or a file-like object with an .open() method.sheetid: The index of the sheet to convert (1-based). Use 0 to convert all sheets.sheetname: The name of the sheet to convert. If provided, sheetid is ignored.If sheetid is 0 and outfile is a directory path, the method will create individual CSV files for each sheet named after the sheet (e.g., Sheet1.csv). If a sheetdelimiter is provided, it will be written between sheet outputs.
When working with Xlsx2csv, be prepared to catch the following exceptions:
InvalidXlsxFileException: Raised when the input file is not a valid XLSX (zip) file.SheetNotFoundException: Raised when the requested sheetid or sheetname does not exist.OutFileAlreadyExistsException: Raised if the output file already exists when attempting to write to a file path.XlsxValueError: Raised when encountering unknown or unhandled data formats.XlsxException: Base exception for all library-specific errors.Full list of available CLI arguments for xlsx2csv:
# Positional Arguments
xlsxfile xlsx file path, use '-' to read from STDIN
outfile output CSV file path (optional)
# Options
-a, --all export all sheets
-c, --outputencoding encoding of output CSV **Python 3 only** (default: utf-8)
-d, --delimiter delimiter - column delimiter in CSV, 'tab' or 'x09' for a tab (default: comma ',')
--hyperlinks include hyperlinks
-e, --escape escape \r\n\t characters
--no-line-breaks replace \r\n\t with space
-E, --exclude_sheet_pattern exclude sheets with names matching the given pattern
-f, --dateformat override date/time format (ex. %%Y/%%m/%%d)
-t, --timeformat override time format (ex. %%H/%%M/%%S)
--floatformat override float format (ex. %%.15f)
--sci-float force scientific notation to float
-I, --include_sheet_pattern only include sheets with names matching the given pattern
--exclude_hidden_sheets exclude hidden sheets from the output
--ignore-formats ignore format for specific data types
-l, --lineterminator line terminator in CSV, '\n' '\r\n' or '\r' (default: \n)
-m, --merge-cells merge cells
-n, --sheetname sheet name to convert
-i, --ignoreempty skip empty lines
--skipemptycolumns skip trailing empty columns
-p, --sheetdelimiter sheet delimiter used to separate sheets (default: '--------')
-q, --quoting quoting - field quoting in CSV, 'none' 'minimal' 'nonnumeric' or 'all'
-s, --sheet sheet number to convert
--include-hidden-rows include hidden rows
--continue-on-error continue processing remaining files when an error occurs during batch processing
--ignore-percentage ignore percentage formatting and output raw valuesYou can control which sheets are exported using several flags. This is particularly useful when using the --all flag to export all sheets in a workbook.
-s, --sheet <id>: Convert a specific sheet by its index (default: 1).-n, --sheetname <name>: Convert a specific sheet by its name.-a, --all: Export all sheets in the workbook.-I, --include_sheet_pattern <pattern>: Only include sheets matching a regex pattern (used with --all).-E, --exclude_sheet_pattern <pattern>: Exclude sheets matching a regex pattern (used with --all).--exclude_hidden_sheets: Exclude hidden sheets (used with --all).Override how specific data types are represented in the output:
-f, --dateformat <format>: Override date/time format (e.g., %%Y/%%m/%%d).-t, --timeformat <format>: Override time format (e.g., %%H/%%M/%%S).--floatformat <format>: Override float format (e.g., %%.15f).--sci-float: Force scientific notation for floats.--ignore-percentage: Output raw decimal values instead of formatted percentages.--ignore-formats <types>: Ignore specific format types (e.g., date, time, float, percentage).