Convert TXT Files to PDF in Python

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. In order 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

Aspose.Words for Python is a powerful and feature-rich library to create and manipulate word processing documents. In addition, it provides a document converter that lets you convert Word documents and plain text files to other formats. We will use this library to convert TXT 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

Get a Free API License

You can get a temporary license to use Aspose.Words for Python without evaluation 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