If you’re looking to convert xlsx to csv in Python, or even handle .xls
files, this guide is for you. CSV files are lightweight, easy to parse, and widely supported across databases and data tools—making them ideal for automation and data exchange.
In this article, we’ll show you how to convert Excel to CSV in Python using Spire.XLS for Python. The library supports .xlsx
, .xls
, and other common Excel spreadsheet formats, and doesn’t rely on any third-party components. You’ll learn how to export the entire workbook, handle multiple worksheets, and ensure encoding—all with clean and efficient Python code.
What’s Covered:
- Convert XLSX to CSV in Python
- Convert Excel (XLS/XLSX) to CSV: All Sheets, Old Files & Batch
- Advanced Conversion: Customize CSV Output in Python
- Bonus Tip: Convert CSV Back to Excel (XLSX)
- Frequently Asked Questions
Convert XLSX to CSV in Python
To follow along, install Spire.XLS for Python (pip install spire.xls) or Free Spire.XLS for Python (pip install spire.xls.free) for lightweight tasks.
The most common use case is converting .xlsx
files to .csv
. Here’s a simple script using Spire.XLS:
from spire.xls import Workbook, FileFormat
# Initialize a workbook and load the xlsx file
workbook = Workbook()
workbook.LoadFromFile("Sample.xlsx")
# Save the workbook as a csv file
workbook.SaveToFile("output/XLSXToCSV.csv", FileFormat.CSV)
workbook.Dispose()
Result:
⚠️ Note: The SaveToFile()
method saves only the first worksheet. If you need to convert all sheets, see the next section.
This method is perfect for simple, single-sheet Excel-to-CSV conversions in Python.
Convert Excel (XLS/XLSX) to CSV: All Sheets, Old Files & Batch
Whether you're working with modern .xlsx
, older .xls
, or multi-sheet Excel files, Spire.XLS offers multiple ways to export everything to CSV in Python.
Export All Worksheets to Separate CSV Files
To export every worksheet into its own CSV file—for example, monthly tabs or department data:
from spire.xls import Workbook, Encoding
# Initialize a workbook and load the Excel file
workbook = Workbook()
workbook.LoadFromFile("Sample.xlsx")
# Iterate through each sheet
for i in range(workbook.Worksheets.Count):
# Save the current sheet to CSV format
sheet = workbook.Worksheets.get_Item(i)
sheet.SaveToFile(f"output/Sheets/Sheet{i}.csv", ",", Encoding.get_UTF8())
workbook.Dispose()
Result:
Each worksheet becomes an individual CSV, ideal for segmented data processing and distribution.
Convert XLS to CSV in Python
Legacy Excel files (.xls
) are still common in many industries. Fortunately, Spire.XLS fully supports them:
workbook = Workbook()
workbook.LoadFromFile("legacy.xls")
workbook.SaveToFile("legacy_output.csv", FileFormat.CSV)
This converts the first worksheet by default. To convert all .xls
sheets, apply the loop method shown above.
You may also like: Convert XLS Files to XLSX Files with Python
Batch Convert Excel Files to CSV
To automate conversion of multiple .xlsx
and .xls
files stored in a folder:
import os
from spire.xls import Workbook, FileFormat
excel_dir = "./excels"
output_dir = "./csvs"
for file in os.listdir(excel_dir):
if file.endswith(".xlsx") or file.endswith(".xls"):
workbook = Workbook()
workbook.LoadFromFile(os.path.join(excel_dir, file))
base_name = os.path.splitext(file)
workbook.SaveToFile(os.path.join(output_dir, base_name + ".csv"), FileFormat.CSV)
Note: This script saves only the first worksheet. Use sheet.SaveToFile()
in a loop if you want all sheets.
Advanced Conversion: Customize CSV Output in Python
Need to export only specific cells, rows, or columns from Excel to CSV? You can read worksheet content manually and write customized CSV output:
import csv
from itertools import chain
from spire.xls import Workbook
# Initialize a Workbook object and load the Excel file
workbook = Workbook()
workbook.LoadFromFile("Sample.xlsx")
# Get the first worksheet
sheet = workbook.Worksheets.get_Item(0)
# Export data to CSV
with open("output/FilteredOutput.csv", "w", newline="", encoding="utf-8") as csvfile:
writer = csv.writer(csvfile)
for row in range(sheet.AllocatedRange.RowCount): # Export rows 1 to 5
row_data = []
for col in chain(range(0, 2), range(3, sheet.AllocatedRange.ColumnCount)): # Export columns A to C
row_data.append(sheet.Range.get_Item(row + 1, col + 1).Value)
writer.writerow(row_data)
Result:
This provides full control for custom Excel to CSV conversion in Python, such as skipping headers or exporting limited ranges.
Explore More: Export & Import Data Between Excel Files and Databases | Python
Bonus Tip: Convert CSV Back to Excel (XLSX)
While this guide focuses on Excel to CSV, you can also convert CSV files back to Excel using Spire.XLS:
from spire.xls import Workbook, FileFormat
workbook = Workbook()
workbook.LoadFromFile("output.csv", ",")
workbook.SaveToFile("restored.xlsx", FileFormat.Version2016)
This is useful for restoring structured Excel files after processing raw CSV data.
Frequently Asked Questions
Q1: How to convert an XLSX file to CSV in Python?
A: You can use Python libraries like Spire.XLS to load the XLSX file and save it as CSV. See the detailed steps in Convert XLSX to CSV in Python.
Q2: How to convert xlsx into csv?
A: Convert XLSX to CSV by loading the file and exporting to CSV format, either saving the first sheet or iterating through all sheets to save separately. Check Convert Excel (XLS/XLSX) to CSV: All Sheets, Old Files & Batch for multi-sheet and batch examples.
Q3: How to convert xlsx to csv from the command line?
A: While this article focuses on Python, you can create Python scripts using Spire.XLS or other libraries and run them from the command line to batch convert XLSX to CSV files. See the Batch Convert Excel Files to CSV section for related code.
Q4: How to convert Excel to text in Python?
A: Converting Excel to text typically means extracting cell values and saving them as CSV or TXT. Using libraries like Spire.XLS or pandas in Python, you can export Excel data into plain text formats easily. For more advanced output, check Advanced Conversion: Customize CSV Output in Python.
Conclusion
In this guide, you’ve learned effective ways to convert XLSX and XLS files to CSV using Python, including exporting entire workbooks or specific sheets, handling batch conversions, and restoring Excel files from CSV. You also gained insights on customizing CSV outputs by selecting precise cell ranges.
Whether you want to automate your Excel to CSV conversion in Python, process legacy Excel files, or streamline data workflows, Spire.XLS provides a robust, code-friendly solution without requiring Microsoft Excel.
Get a Free License for the Full Version
While the free version of Spire.XLS for Python covers basic Python Excel to CSV tasks, if you need the full features and enhanced performance, you can apply for a temporary free license of the full version. This lets you unlock all capabilities and simplify your Python Excel to CSV processing even further.