In data processing workflows, converting Excel files to plain text (TXT) format is a frequent and essential task. Manual copy-pasting—the traditional approach—is not only inefficient but also highly error-prone, especially with large datasets or incompatible formatting. This guide demonstrates how to implement fast, dependency-free Excel-to-TXT conversion using Spire.XLS for Python, boosting data processing efficiency and flexibility.
Installation Instructions
Full version:pip install spire.Xls
Free version:pip install spire.Xls.Free
Basic: Convert a Single Excel Worksheet to TXT
Below is a step-by-step guide to converting the first worksheet of an Excel file to a TXT file, with clear configuration for delimiters and encoding:
1. Load and Read the Excel File
Initialize a Workbook object to load your Excel file (supports .xlsx, .xls, and other common formats):
from spire.xls import *
from spire.xls.common import *
# Initialize Workbook and load the target Excel file
workbook = Workbook()
workbook.LoadFromFile("sample.xlsx")
2. Execute Conversion and Save the TXT File
Extract the target worksheet and save it as a TXT file with configurable delimiters and encoding:
# Select the first worksheet (0-indexed)
target_sheet = workbook.Worksheets[0]
# Save worksheet to TXT: (output path, delimiter, encoding)
target_sheet.SaveToFile("output.txt", "\t", Encoding.get_UTF8())
Key Parameters:
- Delimiter (tab
\t): Defines the separator for columnar data in the TXT file. Tabs (\t) are recommended for readability; commas work for CSV-compatible TXT. - Encoding (
Encoding.get_UTF8()): Specifies the text file encoding.UTF-8is mandatory for proper display of non-ASCII characters (e.g., Chinese, special symbols).
Advanced: Export Multiple Worksheets to Separate TXT Files
For Excel files with multiple worksheets, use the following reusable function to export each worksheet as a uniquely named TXT file (avoids overwriting and improves traceability):
from spire.xls import *
from spire.xls.common import *
import os # Added for cross-platform path handling
def excel_sheets_to_txt(input_file: str, output_folder: str, delimiter: str = "\t") -> None:
"""
Convert all worksheets in an Excel file to separate TXT files.
Args:
input_file: Path to the source Excel file (e.g., "data.xlsx")
output_folder: Path to save TXT files (created if non-existent)
delimiter: Separator for TXT columns (default: tab "\t")
"""
# Create output folder if it doesn't exist
os.makedirs(output_folder, exist_ok=True)
# Initialize Workbook and load Excel file
workbook = Workbook()
workbook.LoadFromFile(input_file)
# Iterate through all worksheets (0-indexed)
for idx in range(workbook.Worksheets.Count):
sheet = workbook.Worksheets[idx]
# Generate unique filename (e.g., "sheet_1_SalesData.txt")
output_filename = f"sheet_{idx+1}_{sheet.Name}.txt"
output_path = os.path.join(output_folder, output_filename)
# Save worksheet to TXT with UTF-8 encoding
sheet.SaveToFile(output_path, delimiter, Encoding.get_UTF8())
# Example usage
excel_sheets_to_txt(input_file="multi_sheet_data.xlsx", output_folder="txt_outputs")
Integrating Excel-to-TXT Conversion into Automated Workflows
Excel-to-TXT conversion is more than a simple format change—it is a critical enabler for end-to-end data automation. With Spire.XLS for Python, you can seamlessly build these high-value use cases:
1. Automated Report Generation
Extract structured data from Excel templates to generate lightweight TXT report summaries. TXT files are ideal for:
- Embedding in automated email notifications (small file size, universal compatibility)
- Integrating with legacy systems that only support plain-text inputs
2. Data Cleaning & Preprocessing
Convert complex Excel files (with merged cells, formulas, or formatting) to standardized TXT files, which serve as a reliable input source for big data tools like:
- Apache Spark/Hadoop (for distributed data processing)
- SQL databases (via bulk import utilities)
- Python data analysis libraries (Pandas, NumPy)
3. Configuration Management
Export system configurations, parameter tables, or lookup lists from Excel to TXT format for:
- Direct reading by shell/Python scripts (no Excel parsing dependencies)
- Version control (Git/SVN) – TXT files have minimal merge conflicts vs. binary Excel files
- Cross-team collaboration (universal readability across OS and tools)
Key Takeaways
This guide provides a production-ready approach to convert Excel to TXT with Python:
- The core workflow (load → select worksheet → save) is lightweight and scalable for small to large datasets.
- The advanced function adds robustness (folder creation, unique filenames) for enterprise-level automation.
- Integrating this conversion into workflows eliminates manual errors and unlocks seamless integration with data pipelines, reporting tools, and configuration management systems.
By adopting these methods, you can streamline data processing workflows and ensure consistent, reliable conversion of Excel data to universally compatible TXT format.
Top comments (0)