Python save text to pdf

Convert Text File to PDF Using Python | FPDF

python text to pdf

The Portable Document Format, or PDF, is everywhere. But it’s still a format that causes headaches for the average person. Sure, you can send a text, Word file, HTML, PowerPoint or any other file. But other formats, while just as easy to attach to an email, aren’t quite as easy to share as PDF. They might not look quite the same when opened on different machines, or can’t be opened on a Mac. There may be font dependencies, or differing page-sizes or other application or user settings that affect appearance. There may be undesirable information such as slide-show notes, metadata or “Track Changes” information that you might not want to share!
That’s why it is very necessary to convert text to PDF and we will show you how to do it using Python.

“What industries badly need is a universal way to communicate documents across a wide variety of machine configurations, operating systems and communication networks. These documents should be viewable on any display and should be printable on any modern printers. If this problem can be solved, then the fundamental way people work will change.”

— John E. Warnock, the co-founder of Adobe,

So, if you don’t know how to convert a text file to PDF using Python then this article is for you. In this article, you will come to know the way to convert text and text file to PDF in Python.

Читайте также:  Моё Node-приложение!

Why Choose PDF Over Text File

There are tons of reasons to choose PDF over a Python text file. We are Mentioning few of them:

  • Easy
  • Portability
  • Flexibility
  • Security
  • Authentication
  • Semantics
  • Non-Proprietary

Converting Text to PDF Using Python

As we all know, python is an ocean of libraries. There are many libraries which can convert a text or a text file to PDF. But in this specific article, we are using the simplest and most popular library to achieve our goal, which is to convert a text file to PDF.

So, enough of talks we are revealing the name of the library we are using and that is FPDF. The PyFPDF package is a port of the “Free”-PDF package that was written in PHP.

Main Features of FPDF

The main features of PYFDF are:

  • Easy to use (and easy to extend)
  • Many simple examples and scripts available in many languages
  • No external dependencies or extensions (optionally PIL for GIF support)
  • No installation, no compilation or other libraries (DLLs) required
  • Small and compact code, useful for testing new features and teaching

Installation

Installing PyFPDF is easy since it was designed to work with pip. Here’s how:

Support

The FPDF supports Python 2.5+ to 3.4+ all versions.

Now, let’s directly move to various programs to convert Python text to PDF.

Python Program to Convert Text to PDF

Prerequisites

  • To run the below Python script, you must already have the latest version of Python 3.x installed on your device.
  • This example uses the FPDF library to generate the PDF. Therefore, install it before you start.

Steps

Step 1: import the library package’s class FPDF :

#Importing the FPDF library. from fpdf import FPDF

Let’s move to the next step.

Step 2: Saving FPDF class into a variable named PDF and adding a page:

Step 3: Setting the style and size of font that you want in the PDF:

pdf.set_font("Arial", size = 15) 

You can change the font and font size according to your need.

pdf.cell(200, 10, txt = "Python Pool", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Start your Python Learning journey now with Python Pool", ln = 2, align = 'C') 

Note:

If you go diving into the source, you will find that the PyFPDF package only supports the following page sizes:

The PyFPDF has a set of core fonts hard-coded into its FPDF class:

Writing the whole program in a single window:

#Importing the FPDF library. from fpdf import FPDF pdf = FPDF() pdf.add_page() pdf.set_font("Arial", size = 15) pdf.cell(200, 10, txt = "Python Pool", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Start your Python Learning journey now with Python Pool", ln = 2, align = 'C') pdf.output("PP.pdf") 

Converted PDF

Converting a Whole Text File to PDF Using Python and FPDF

In the above example, we have learned how to convert a text or a phrase of text to PDF. But in this advance python script, we will learn how to convert the whole.txt file.

Firstly we need a plain text file. Which you can create in notepad itself.
And save it with .txt extension. Like the example shown below.

Input Text

# Python program to convert text file to PDF using FPDF from fpdf import FPDF pdf = FPDF() pdf.add_page() pdf.set_font("Arial", size = 15) f = open("input.txt", "r") for x in f: pdf.cell(200, 10, txt = x, ln = 1, align = 'C') pdf.output("purepython.pdf") 

Python Text to PDF

converted text file

Other Libraries to Convert Text to PDF

There are many libraries available in Python’s Galaxy to convert text to PDF some of them are:

Must Read:

Conclusion

The creation of PDF file is frequently done whether you are a student or a working professional. In this article, we learned how to convert Python text to PDF with the help of FPDF package. FPDF is a fairly nice project that lets you do basic PDF generation. Now if you have followed all the steps properly. You don’t need to buy a tool to convert PDF. You can directly use this script and get the job done.

Try to run the programs on your side and let us know if you have any queries.

Happy Coding!

Источник

Convert Word DOCX or DOC to PDF in Python

Word to PDF Conversion Python

Word to PDF is one of the most popular and immensely performed document conversions. The DOCX or DOC files are converted to PDF format before they are printed or shared. In this article, we will automate Word to PDF conversion in Python. The steps and code samples will demonstrate how to convert Word DOCX or DOC to PDF with Python. Also, you will learn about different options to customize Word to PDF conversion.

Python Library for Word to PDF Conversion — Free Download#

For converting Word documents to PDF format, we will use Aspose.Words for Python. It is a feature-rich Python library for creating and manipulating Word documents. Moreover, it lets you convert DOCX and DOC files to PDF format with high fidelity. The library is hosted on PyPI and you can install it using the following pip command.

Convert Word DOCX to PDF in Python#

The following are the steps to convert a Word document to PDF in Python.

  • Load the Word document using Document class.
  • Convert Word document to PDF using Document.save() method.

The following code sample shows how to convert a Word DOCX file to PDF.

Python Word to PDF with a Particular Standard#

You can also specify the particular standard for the converted PDF document such as PDF/A. The following are the steps to specify the PDF standard in Word to PDF conversion using Python.

  • Load the Word document using Document class.
  • Create an object of PdfSaveOptions class and set PDF standard using PdfSaveOptions.compliance property.
  • Convert Word document to PDF using Document.save() method.

The following code sample shows how to set a particular standard in Word DOCX to PDF conversion.

Python DOCX to PDF — Convert Range of Pages#

You can also specify the range of pages you want to convert to PDF format. For this, you can use PdfSaveOptions.page_set property. The following code sample shows how to convert a range of pages in Word document to PDF.

DOC DOCX to PDF in Python — Apply Image Compression#

Aspose.Words for Python also lets you apply image compression in the converted PDF document. In addition, you can specify the JPEG quality for the images. The following are the steps to set image compression while converting a Word DOCX to PDF in Python.

  • Load the Word document using Document class.
  • Create an object of PdfSaveOptions class.
  • Set image compression using PdfSaveOptions.image_compression property.
  • Set JPEG quality using PdfSaveOptions.jpeg_quality property.
  • Convert Word document to PDF using Document.save() method.

The following code sample shows how to set image compression in Word to PDF conversion.

Python DOCX to PDF Library — Get a Free Library License#

You can get a temporary license in order to use Aspose.Words for Python without evaluation limitations.

Conclusion#

In this article, you have learned how to convert Word DOCX or DOC files to PDF in Python. Moreover, you have seen different options to customize the DOC or DOCX to PDF conversion in Python. You can learn more about Aspose.Words for Python using documentation. In case you would have any questions, feel free to let us know via our forum.

See Also#

Источник

Оцените статью