Python: Convert RTF to PDF, HTML

RTF (Rich Text Format) is a versatile file format that can be opened and viewed by various word processing software. It supports a wide range of text formatting options, such as font style, size, color, tables, images, and more. When working with RTF files, you may sometimes need to convert them to PDF files for better sharing and printing, or to HTML format for publishing on the web. In this article, you will learn how to convert RTF to PDF or HTML with Python using Spire.Doc for Python.

Install Spire.Doc for Python

This scenario requires Spire.Doc for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip commands.

pip install Spire.Doc

If you are unsure how to install, please refer to this tutorial: How to Install Spire.Doc for Python on Windows

Convert RTF to PDF in Python

To convert an RTF file to PDF, simply load a file with .rtf extension and then save it as a PDF file using Document.SaveToFile(fileName, FileFormat.PDF) method. The following are the detailed steps.

  • Create a Document object.
  • Load an RTF file using Document.LoadFromFile() method.
  • Save the RTF file as a PDF file using Document.SaveToFile(fileName, FileFormat.PDF) method.
  • Python
from spire.doc import *
from spire.doc.common import *

inputFile = "input.rtf"
outputFile = "RtfToPDF.pdf"

# Create a Document object
doc = Document()

# Load an RTF file from disk
doc.LoadFromFile(inputFile)

# Save the RTF file as a PDF file
doc.SaveToFile(outputFile, FileFormat.PDF)
doc.Close()

Python: Convert RTF to PDF, HTML

Convert RTF to HTML in Python

Spire.Doc for Python also allows you to use the Document.SaveToFile(fileName, FileFormat.Html) method to convert the loaded RTF file to HTML format. The following are the detailed steps.

  • Create a Document object.
  • Load an RTF file using Document.LoadFromFile() method.
  • Save the RTF file in HTML format using Document.SaveToFile(fileName, FileFormat.Html) method.
  • Python
from spire.doc import *
from spire.doc.common import *

inputFile = "input.rtf"
outputFile = "RtfToHtml.html"
               
# Create a Document object
doc = Document()

# Load an RTF file from disk
doc.LoadFromFile(inputFile)

# Save the RTF file in HTML format
doc.SaveToFile(outputFile, FileFormat.Html)
doc.Close()

Python: Convert RTF to PDF, HTML

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.