DEV Community

jelizaveta
jelizaveta

Posted on

Using Python to Export Excel to TXT

During data processing and analysis, it is often necessary to convert data between different formats. Excel files are a very common format for data storage and manipulation, while TXT files, with their simple text format, are often used for data sharing and processing. This article will introduce how to use Python and the Spire.XLS library to export Excel files to TXT format.

Environment Setup

To achieve this functionality, we need to ensure that the Spire.XLS for Python library is installed. If it’s not installed yet, you can do so with the following command:

pip install Spire.XLS
Enter fullscreen mode Exit fullscreen mode

This library provides a rich set of features for handling Excel files, making it easy to read, edit, and save.

Example Code

Below is a complete example code that demonstrates how to export an Excel file to a TXT file:

import os
import sys

# Get the current file path
curPath = os.path.abspath(os.path.dirname(__file__))
rootPath = os.path.split(curPath)[0]
sys.path.append(rootPath)

from spire.xls import *
from spire.xls.common import *

# Input and output file paths
inputFile = "Input.xlsx"
outputFile = "output.txt"

# Create a Workbook object 
workbook = Workbook()

# Load an Excel file
workbook.LoadFromFile(inputFile)

# Get the first worksheet
sheet = workbook.Worksheets[0]

# Save the worksheet as a TXT file
sheet.SaveToFile(outputFile, " ", Encoding.get_UTF8())
workbook.Dispose()
Enter fullscreen mode Exit fullscreen mode

Code Explanation

1.Environment Configuration

We first import the necessary modules to prepare for the file operations. Using os and sys modules, we obtain the current file path for file import and export.

2.Create Workbook Object

We create a new workbook object using the Workbook() class. This is the foundation for operating on Excel files.

3.Load Excel File

We load the specified Excel file via the LoadFromFile method. In this example, the file name is "Input.xlsx".

4.Get Worksheet

An Excel file can contain multiple worksheets. Here, we access the first worksheet using workbook.Worksheets[0]. The index starts at 0, so [0] represents the first worksheet.

5.Export to TXT File

We use the SaveToFile method to export the worksheet as a TXT file. Here, we set the output file name and the column separator (using a space " "). We also specify the file encoding as UTF-8 to ensure proper display of various language characters.

6.Release Resources

Finally, we use the Dispose() method to release the resources occupied by the workbook, ensuring program stability.

Conclusion

By following the above steps, we successfully exported an Excel file to TXT format using Python. Spire.XLS provides straightforward methods that make working with Excel files very simple, especially suitable for scenarios requiring batch processing or automation scripts.

For more complex needs, such as handling multiple worksheets or formatting and filtering data, the code logic can be further improved with additional functionality. Additionally, Spire.XLS supports other flexible operations on Excel files, such as modifying cell contents, adding charts, and more, allowing users to explore the library's capabilities according to their needs.

Top comments (0)