DEV Community

Cover image for How to Convert Excel Workbook to Text in Python
Aspose for GroupDocs.Cloud

Posted on Originally published at blog.groupdocs.cloud

How to Convert Excel Workbook to Text in Python

Extracting raw text from Excel workbooks is a frequent need when building data pipelines or generating simple reports. If you need to know how to convert Excel Workbook to text in Python, the GroupDocs.Conversion Cloud SDK for Python makes it straightforward. This guide walks you through installing the SDK, uploading a workbook, configuring conversion options, and retrieving the plain TXT result. By the end you’ll have a reusable script and a set of cURL commands for REST‑based automation.

Before You Start: Prerequisites and Installation

Before you begin, ensure you have the following:

  • Python 3.7 or newer installed on your machine.
  • An active GroupDocs Cloud account (client ID and client secret).
  • Network access to the GroupDocs Cloud endpoints.

Install the SDK via pip:

pip install groupdocs-conversion-cloud
Enter fullscreen mode Exit fullscreen mode

You can also download the latest package from the official release page. After installation, set your credentials as environment variables or replace the placeholders in the code later. With the SDK ready, you’re prepared to start the conversion process.

Building It Step by Step: How to Convert Excel Workbook to Text in Python

Step 1: Configure Credentials

First, create a Configuration object and supply your client ID and secret. This object is used by all subsequent API calls.

import os
from groupdocs_conversion_cloud import Configuration

client_id = os.getenv("GROUPDOCS_CLIENT_ID", "YOUR_CLIENT_ID")
client_secret = os.getenv("GROUPDOCS_CLIENT_SECRET", "YOUR_CLIENT_SECRET")

config = Configuration()
config.client_id = client_id
config.client_secret = client_secret
Enter fullscreen mode Exit fullscreen mode

The Configuration class is documented in the API reference.

Step 2: Upload Excel Workbook

Upload the local Excel file to GroupDocs Cloud storage. This step is optional if your file is already in the cloud.

from groupdocs_conversion_cloud import ApiClient, UploadFileApi, UploadFileRequest

api_client = ApiClient(config)
upload_api = UploadFileApi(api_client)

local_excel_path = "sample.xlsx"
remote_excel_path = "sample.xlsx"

with open(local_excel_path, "rb") as f:
    upload_request = UploadFileRequest(
        path=remote_excel_path,
        file=f.read()
    )
    upload_api.upload_file(upload_request)
Enter fullscreen mode Exit fullscreen mode

The UploadFileApi handles file transfers; see its documentation for more details.

Step 3: Define Conversion Options

Create a TxtConvertOptions object to specify any optional parameters such as page range. Then build the ConvertSettings object that ties everything together.

from groupdocs_conversion_cloud import (
    FileInfo,
    TxtConvertOptions,
    ConvertSettings,
)

file_info = FileInfo()
file_info.file_path = remote_excel_path

txt_options = TxtConvertOptions()
# txt_options.page_number = 1   # Uncomment to start from a specific page
# txt_options.page_count = 1    # Uncomment to limit the number of pages

convert_settings = ConvertSettings()
convert_settings.file_info = file_info
convert_settings.format = "txt"
convert_settings.output_path = "sample.txt"
convert_settings.options = txt_options
Enter fullscreen mode Exit fullscreen mode

The TxtConvertOptions class lets you fine‑tune the conversion; refer to the official documentation for all available fields.

Step 4: Execute Conversion

Call the convert method of ConvertApi with the prepared settings. The response contains a URL to download the converted file.

from groupdocs_conversion_cloud import ConvertApi, ApiException

convert_api = ConvertApi(api_client)

try:
    conversion_result = convert_api.convert(convert_settings)
    print(f"Conversion succeeded. Download URL: {conversion_result.url}")
except ApiException as e:
    print(f"Conversion failed: {e}")
Enter fullscreen mode Exit fullscreen mode

If the call succeeds, you’ll receive a direct download link for the TXT file.

Step 5: Retrieve Result

Use the URL from the previous step to download the plain text file to your local environment, or process it further in your application.

Tip: You can automate the download with a simple requests.get() call, handling authentication via the same access token used for the conversion request.

With these steps, you now have a complete workflow for how to convert Excel Workbook to text in Python using the GroupDocs library.

Excel Workbook Text Conversion - Complete Code Example

The following example demonstrates the full implementation from start to finish.

import os
from groupdocs_conversion_cloud import (
    Configuration,
    ApiClient,
    UploadFileApi,
    UploadFileRequest,
    ConvertApi,
    ConvertSettings,
    FileInfo,
    TxtConvertOptions,
    ApiException,
)

def main():
    # ----------------------------------------------------------------------
    # 1. Configure credentials (replace placeholders or set env variables)
    # ----------------------------------------------------------------------
    client_id = os.getenv("GROUPDOCS_CLIENT_ID", "YOUR_CLIENT_ID")
    client_secret = os.getenv("GROUPDOCS_CLIENT_SECRET", "YOUR_CLIENT_SECRET")

    config = Configuration()
    config.client_id = client_id
    config.client_secret = client_secret

    api_client = ApiClient(config)

    # ----------------------------------------------------------------------
    # 2. Upload local Excel workbook to GroupDocs Cloud storage (optional)
    # ----------------------------------------------------------------------
    local_excel_path = "sample.xlsx"          # path to the Excel file on your machine
    remote_excel_path = "sample.xlsx"         # path where the file will be stored in the cloud

    upload_api = UploadFileApi(api_client)
    try:
        with open(local_excel_path, "rb") as f:
            upload_request = UploadFileRequest(
                path=remote_excel_path,
                file=f.read()
            )
            upload_api.upload_file(upload_request)
        print(f"Uploaded '{local_excel_path}' to cloud storage as '{remote_excel_path}'.")
    except (ApiException, IOError) as e:
        print(f"Failed to upload file: {e}")
        return

    # ----------------------------------------------------------------------
    # 3. Prepare conversion settings to transform Excel → plain text
    # ----------------------------------------------------------------------
    convert_api = ConvertApi(api_client)

    file_info = FileInfo()
    file_info.file_path = remote_excel_path          # source file in cloud storage

    txt_options = TxtConvertOptions()
    # (optional) you can limit conversion to specific pages:
    # txt_options.page_number = 1
    # txt_options.page_count = 1

    convert_settings = ConvertSettings()
    convert_settings.file_info = file_info
    convert_settings.format = "txt"                  # target format
    convert_settings.output_path = "sample.txt"      # result file name in cloud storage
    convert_settings.options = txt_options

    # ----------------------------------------------------------------------
    # 4. Execute conversion
    # ----------------------------------------------------------------------
    try:
        conversion_result = convert_api.convert(convert_settings)
        # The result contains a URL to download the converted file
        print(f"Conversion succeeded. Download URL: {conversion_result.url}")
    except ApiException as e:
        print(f"Conversion failed: {e}")

if __name__ == "__main__":
    main()
Enter fullscreen mode Exit fullscreen mode

Note: This code example demonstrates the core functionality. Before using it in your project, make sure to update any file paths and configuration values to match your actual environment, verify that all required dependencies are properly installed, and test thoroughly in your development environment. If you encounter any issues, please refer to the official documentation or reach out to the support team for assistance.

Excel to Text Conversion Using cURL and the REST API

If you prefer a pure REST approach, the same conversion can be performed with cURL commands.

  1. Obtain an access token
   curl -X POST "https://api.groupdocs.cloud/v2.0/connect/token" \
        -H "Content-Type: application/x-www-form-urlencoded" \
        -d "grant_type=client_credentials&client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET"
Enter fullscreen mode Exit fullscreen mode
  1. Upload the source Excel file
   curl -X PUT "https://api.groupdocs.cloud/v2.0/storage/file/sample.xlsx" \
        -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
        -H "Content-Type: application/octet-stream" \
        --data-binary @sample.xlsx
Enter fullscreen mode Exit fullscreen mode
  1. Start the conversion to TXT
   curl -X POST "https://api.groupdocs.cloud/v2.0/conversion/convert" \
        -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
        -H "Content-Type: application/json" \
        -d '{
              "fileInfo": { "filePath": "sample.xlsx" },
              "format": "txt",
              "outputPath": "sample.txt",
              "options": {}
            }'
Enter fullscreen mode Exit fullscreen mode
  1. Download the converted TXT file
   curl -X GET "https://api.groupdocs.cloud/v2.0/storage/file/sample.txt" \
        -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
        -o sample.txt
Enter fullscreen mode Exit fullscreen mode

These commands illustrate the full lifecycle from authentication to download. For additional parameters and error handling, consult the API reference.

Configuring Conversion Settings for Excel

The SDK offers several options to tailor the output. Below are the most useful settings for Excel‑to‑TXT conversion:

  • Page Number - Start conversion from a specific sheet or page.
   txt_options.page_number = 1
Enter fullscreen mode Exit fullscreen mode
  • Page Count - Limit the number of sheets/pages to convert.
   txt_options.page_count = 2
Enter fullscreen mode Exit fullscreen mode
  • Output Path - Define the name and location of the resulting TXT file in cloud storage.
   convert_settings.output_path = "output/sample.txt"
Enter fullscreen mode Exit fullscreen mode

Other advanced options such as encoding or preserveFormatting are available in the API reference; explore them to match your exact requirements.

Final Thoughts

Converting an Excel workbook to plain text in Python is now a simple, repeatable process thanks to the GroupDocs.Conversion Cloud SDK for Python. The SDK handles authentication, file storage, and conversion behind the scenes, letting you focus on business logic. Remember to review the licensing options on the pricing page and obtain a temporary license for evaluation before moving to production. With the code and cURL examples provided, you can integrate Excel‑to‑TXT conversion into any Python‑based workflow or microservice.

Common Questions

  • What is the easiest way to convert an Excel workbook to text in Python? Use the GroupDocs.Conversion Cloud SDK for Python. It abstracts the REST calls and provides a clean Pythonic API for uploading, converting, and downloading files.
  • Can I convert only specific sheets or pages of an Excel workbook? Yes. Set page_number and page_count on the TxtConvertOptions object to target particular sheets. See the API reference for full details.
  • Do I need a GroupDocs account to use the SDK? A valid GroupDocs Cloud account with client ID and secret is required. You can create an account on the GroupDocs portal and obtain credentials from the dashboard.
  • Is there a file‑size limit for Excel to TXT conversion? The cloud service supports files up to 200 MB for conversion. Larger files should be split or processed in chunks before uploading.

Further Reading

Top comments (0)