Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Convert Text to HTML and HTML to Text in Python
# How to Convert HTML to Plain Text Online in Python using REST API
# Create necessary API instances
convert_api = groupdocs_conversion_cloud.ConvertApi.from_keys(client_id, client_secret)
# Prepare convert settings
settings = groupdocs_conversion_cloud.ConvertSettings()
settings.file_path = "python-testing/input-sample-file.html"
settings.format = "txt"
settings.output_path = "python-testing/output-sample-file.txt"
# Create convert document request
request = groupdocs_conversion_cloud.ConvertDocumentRequest(settings)
# Convert HTML file to Text file
result = convert_api.convert_document(request)
print("Successfully converted HTML file to Text file online in Python: " + result[0].path)
# How to Convert Text to HTML Online in Python using REST API
# Create necessary API instances
convert_api = groupdocs_conversion_cloud.ConvertApi.from_keys(client_id, client_secret)
# Prepare convert settings
settings = groupdocs_conversion_cloud.ConvertSettings()
settings.file_path = "python-testing/input-sample-file.txt"
settings.format = "html"
settings.output_path = "python-testing/output-sample-file.html"
# Create convert document request
request = groupdocs_conversion_cloud.ConvertDocumentRequest(settings)
# Convert Text file to HTML file
result = convert_api.convert_document(request)
print("Successfully converted Text file to HTML web page in Python: " + result[0].path)
# Import Python SDK in your python application from http://api.groupdocs.cloud
import groupdocs_conversion_cloud
# Get client_id and client_secret from https://dashboard.groupdocs.cloud after free registration.
client_id = "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
client_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
# Get File API configurations
configuration = groupdocs_conversion_cloud.Configuration(client_id, client_secret)
configuration.api_base_url = "https://api.groupdocs.cloud"
storage_name = "LocalStorage"

You can convert Text to HTML and HTML to Text format programmatically on the cloud. In this article, you will learn how to convert Text to HTML and HTML to Text in Python.

The following topics shall be covered in this article:

  1. Text to HTML and HTML to Text Conversion REST API – Installation
  2. How to Convert Text to HTML Online in Python using REST API
  3. Convert Text File to HTML using Advanced Options in Python
  4. Convert HTML to Plain Text Online in Python using REST API
# Convert Text File to HTML using Advanced Options in Python
# Create necessary API instances
convert_api = groupdocs_conversion_cloud.ConvertApi.from_keys(client_id, client_secret)
# Prepare convert settings
settings = groupdocs_conversion_cloud.ConvertSettings()
settings.file_path = "python-testing/input-sample-file.txt"
settings.format = "html"
settings.output_path = "python-testing/output-sample-file.html"
# HTML convert options
convertOptions = groupdocs_conversion_cloud.HtmlConvertOptions()
convertOptions.from_page = 1
convertOptions.pages_count = 1
convertOptions.fixed_layout = True
convertOptions.use_pdf = False
settings.convert_options = convertOptions
# Create convert document request
request = groupdocs_conversion_cloud.ConvertDocumentRequest(settings)
# Convert Text file to HTML file
result = convert_api.convert_document(request)
print("Successfully converted Text file to HTML file online in Python: " + result[0].path)
# API initialization to download converted file
import shutil
file_api = groupdocs_conversion_cloud.FileApi.from_config(configuration)
# Create download file request
request = groupdocs_conversion_cloud.DownloadFileRequest("python-testing\\output-sample-file.html", storage_name)
# Download converted file
response = file_api.download_file(request)
# Move the downloaded file to your directory
shutil.move(response, "H:\\groupdocs-cloud-data\\")
# Upload Input file to cloud storage
# Create an instance of the file API
file_api = groupdocs_conversion_cloud.FileApi.from_config(configuration)
# Call upload file request
request = groupdocs_conversion_cloud.UploadFileRequest("python-testing\input-sample-file.txt", "H:\\groupdocs-cloud-data\\input-sample-file.txt", storage_name)
# Upload sample file to the cloud
response = file_api.upload_file(request)
print(response.uploaded)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment