Convert a TXT file to PDF in Python

Various people use Notepad to write down important points or create notes quickly in TXT format. Also, TXT files are used to store plain text in various applications. However, since Notepad does not provide advanced features, TXT files are often converted to PDF. To automate TXT to PDF conversion programmatically, this article covers how to convert TXT files to PDF format in Python.

Python Library for TXT to PDF Conversion

We will use Aspose.Words for Python for TXT to PDF conversion. It is a powerful and feature-rich library to create and manipulate word-processing documents. In addition, it provides a free, high-speed and high-quality converter to convert plain text files to PDF format.

You can install it from PyPI using the following pip command.

pip install aspose-words

Convert a TXT File to PDF in Python

Aspose.Words makes TXT to PDF conversion quite easy for you. Simply load the TXT file and save it as a PDF document to your desired location. The following steps demonstrate how to convert a TXT file to PDF in Python.

  • Load TXT file using Document class.
  • Save TXT file as PDF using Document.save(string, SaveFormat) method.

The following code sample shows how to convert a TXT file to PDF programmatically.

The following is the screenshot of the converted PDF file.

Convert TXT files to PDF programmatically using Python

Converted PDF File

Free TXT to PDF Converter

You can get a free temporary license to save your TXT files in PDF format without any limitations.

Conclusion

In this article, you have learned how to convert TXT files to PDF format programmatically in Python. You can install Aspose.Words and automate TXT to PDF conversion from within your applications. In case you want to learn more about Aspose.Words for Python, visit the documentation. Moreover, you can post your queries or questions to our forum.

See Also