Skip to content

Instantly share code, notes, and snippets.

@groupdocs-cloud-gists
Last active July 20, 2022 14:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save groupdocs-cloud-gists/a7229a7a958a3b158b7508359b2d2aae to your computer and use it in GitHub Desktop.
Save groupdocs-cloud-gists/a7229a7a958a3b158b7508359b2d2aae to your computer and use it in GitHub Desktop.
Convert Text Files to PDF using File Conversion API in Python

Learn how to convert text file to PDF document in Python using REST API:

The following topics shall be covered in this article:

  1. Text to PDF Conversion REST API and Python SDK
  2. How to Convert Text to PDF using REST API in Python
  3. Convert Text to PDF with Advanced Options in Python
  4. Convert Range of Pages from Text to PDF in Python
  5. Convert Specific Pages of Text to PDF in Python
# How to Convert Range of Pages from Text to PDF in Python
# Create an instance of API
convert_api = groupdocs_conversion_cloud.ConvertApi.from_keys(client_id, client_secret)
# Define convert settings
settings = groupdocs_conversion_cloud.ConvertSettings()
settings.file_path = "python-testing/sample-text-file.txt"
settings.format = "pdf"
settings.output_path = "python-testing"
# PDF convert options: start page number and total pages to convert
convertOptions = groupdocs_conversion_cloud.PdfConvertOptions()
convertOptions.from_page = 1
convertOptions.pages_count = 2
settings.convert_options = convertOptions
# Create convert document request
request = groupdocs_conversion_cloud.ConvertDocumentRequest(settings)
# Convert pages of text file to PDF file
result = convert_api.convert_document(request)
print("Converted range of pages from Text file to PDF: " + result[0].path)
# How to Convert Specific Pages of Text to PDF in Python
# Create an instance of API
convert_api = groupdocs_conversion_cloud.ConvertApi.from_keys(client_id, client_secret)
# Define convert settings
settings = groupdocs_conversion_cloud.ConvertSettings()
settings.file_path = "python-testing/sample-text-file.txt"
settings.format = "pdf"
settings.output_path = "python-testing"
# PDF convert options: page numbers to convert
convertOptions = groupdocs_conversion_cloud.PdfConvertOptions()
convertOptions.pages = [1, 3]
settings.convert_options = convertOptions
# Create convert document request
request = groupdocs_conversion_cloud.ConvertDocumentRequest(settings)
# Convert text file to PDF file
result = convert_api.convert_document(request)
print("Successfully converted Text file pages to PDF: " + result[0].path)
# How to Convert Text to PDF using REST API in Python
try:
# Create an instance of the API
convert_api = groupdocs_conversion_cloud.ConvertApi.from_keys(client_id, client_secret)
# Define convert settings
settings = groupdocs_conversion_cloud.ConvertSettings()
settings.file_path = "python-testing/sample-text-file.txt"
settings.format = "pdf"
settings.output_path = "python-testing"
# Create convert document request
request = groupdocs_conversion_cloud.ConvertDocumentRequest(settings)
# Convert .txt file to PDF document
result = convert_api.convert_document(request)
print("TXT File converted to PDF successfully: " + result[0].path)
except groupdocs_conversion_cloud.ApiException as e:
print("Exception when calling convert_document: {0}".format(e.message))
# Load Python SDK http://api.groupdocs.cloud in your python application
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 different configurations
configuration = groupdocs_conversion_cloud.Configuration(client_id, client_secret)
configuration.api_base_url = "https://api.groupdocs.cloud"
my_storage = "LocalStorage"
# API initialization to download converted file
file_api = groupdocs_conversion_cloud.FileApi.from_config(configuration)
# Create download file request
request = groupdocs_conversion_cloud.DownloadFileRequest("python-testing\\sample-text-file.pdf", my_storage)
# Download converted file
response = file_api.download_file(request)
# Move the downloaded file to your directory
shutil.move(response, "H:\\groupdocs-cloud-data\\")
# Upload sample .txt file on the cloud storage
# Create an instance of the File API
file_api = groupdocs_conversion_cloud.FileApi.from_config(configuration)
# Upload file request
request = groupdocs_conversion_cloud.UploadFileRequest("python-testing\sample-text-file.txt", "H:\\groupdocs-cloud-data\\sample-text-file.txt", my_storage)
# Upload sample text file
response = file_api.upload_file(request)
# Convert Text to PDF using Advanced Options in Python
# Create an instance of the API
convert_api = groupdocs_conversion_cloud.ConvertApi.from_keys(client_id, client_secret)
# Define convert settings
settings = groupdocs_conversion_cloud.ConvertSettings()
settings.file_path = "python-testing/sample-text-file.txt"
settings.format = "pdf"
settings.output_path = "python-testing"
# Text load options
loadOptions = groupdocs_conversion_cloud.TxtLoadOptions()
loadOptions.encoding = "shift_jis"
# Set PDF convert options
convertOptions = groupdocs_conversion_cloud.PdfConvertOptions()
convertOptions.center_window = True
convertOptions.display_doc_title = True
convertOptions.dpi = 1024.0
convertOptions.fit_window = False
convertOptions.grayscale = False
convertOptions.linearize = False
convertOptions.margin_top = 5
convertOptions.margin_left = 5
convertOptions.unembed_fonts = True
convertOptions.remove_pdfa_compliance = False
settings.convert_options = convertOptions
# Create convert document request
request = groupdocs_conversion_cloud.ConvertDocumentRequest(settings)
# Convert pages of text file to PDF file
result = convert_api.convert_document(request)
print("Successfully Converted TXT to PDF with advanced options: " + result[0].path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment