Tutorials
python

Reading and Editing PDF’s and Word Documents From Python

This tutorial will allow you to read PDF documents and merge multiple PDF files into one PDF file. It will also show how to read and write word documents from Python.

PDF Documents

PDF is a Portable Document Format where it contains texts, images, charts, etc. which is different from plain text files. It is a file that contains the '.pdf.' extension and was invented by Adobe. This type of file is independent of any platforms like software, hardware, and operating systems.

Steps to Install-Package:

  1. You need to install a package named "pypdf2" which can handle the file with '.pdf' extension.

  2. You can see the 'pypdf2' package is installed and shown below.

Reading PDF documents and Extracting a data

You will be extracting only the text from the pdf file as PyPDF2 has a limitation when it comes to extracting the rich media content. The logos, pictures, etc. couldn't be extracted from it — the following pdf file needs to be download to work with this tutorial.
Download Pdf file

The 'import' statement in the code above gets the PyPDF2 module. You need to use 'open('pdfFileName' , 'openingMode')'where the 'pdfFilename' is 'test.pdf', and the 'openingMode' is 'rb' which is the reading only in binary format.

The PyPDF2 has a method as 'PdfFileReader', which takes the newly created object 'pdfFileObject'.You can now access the attribute named 'numPages' from 'pdfFileObject', which gives a total number of the pages.

The above output is 1.Since; you can see the pdf file is of only one page.

You can use the 'getPage(0)' method inside the pdfReaderObject to get the first page.The result then is stored in the 'firstPageObject' where all the text inside that particular page can be printed out by using the 'extractText()' method.

The above code gives all the text from the pdf file. However, the image is not shown in the terminal, which cannot be obtained using pyPDF2.

Merging multiple PDF files into one PDF

You will be merging two different pdf files into a single pdf file. The old PDF file is previous that you've worked with, whereas a new PDF file can be downloaded from the following link:
New PDF file.

You will be importing the PdfFileMerger module from the PyPDF2 package, which helps to merge the pdf files. The 'path' is specified, which indicates the path for the folder where the file is located. Also, pdf files to merge are included in 'pdf_files' in a list.

You can see the merger object is created using the help of 'PdfFileMerger.' The looping is done for each file in a list where merging is done by passing the path and file to the 'append' method. At last, the final output can be obtained by using 'merger.write()' where the merged content with a new PDF filename is obtained.

The above picture indicates a 'merged.pdf,' which consists of the content merged from 'test.pdf' and 'test-1.pdf'.

Word Documents

The Word documents consist of the ".docx" extension at the end of the filename. These documents don't only contain text as in plain text files, but it includes a rich-text document. The rich-text document contains the different structures for the document, which have size, align, color, pictures, font, etc. associated with them.

It would be best if you had an application for working with the Word Documents. The popular application for Windows and Mac Operating systems is Microsoft Word, but it is a paid subscription platform. However, there is a free alternative option like "LibreOffice", which is an application in Linux which comes pre-installed. The applications can be downloaded for Windows and Mac Operating systems. This tutorial will use Microsoft Word in the Windows Operating system.

Steps to Install-Package:

  1. You need to install a package named "python-docx" which can handle the word documents of the '.docx' extension.

  2. You can see the 'python-docx' package installed and shown below.

You can code along in the interactive shell provided by Python, but it is preferred to use the Text Editor. So, Sublime Text is used for the coding part of this tutorial.

Writing a Word Documents

You can see above the 'document' module is imported from the 'docx' package in the first line. The code in the second line produces a new word document through the help of the Document object.The filename is saved as 'first.docx' using the 'document.save()'.

Add a heading

The above code contains a 'Document()'opens a new file and 'document.save('addHeader.docx')' is used to create a newly edited docx file. You can add the heading through method 'add_heading('text,' level=number),' which takes the text as a heading, and the heading level from starts from 0 to 4.

The above code gives the output as a newly created 'addedHeader.docx' file where level 0 makes a headline with the horizontal line below the text, whereas the heading level 1 is the main heading. Similarly, the other headings are sub-heading with their's font-sizes in decreasing order.

Add a Paragraph

The above code contains a 'Document()' which opens a new document file and 'document.save('addParagraph.docx')' is used to create a newly edited docx file. You can add the heading through method 'add_paragraph('text,' style='required_style')' which takes the text and also 'style' is an optional argument where 'List Number' and 'List Bullet' can be used.

The above code gives the output as a newly created 'addedParagraph.docx' file where there is a simple paragraph at first line. Similarly, there is a heading, and below it contains an ordered list containing an item with number 1 and 2.Below that there is another heading containing a two item in an unordered bulleted list.

Add a Picture

The above code contains a 'Document(),' which creates a new document file, and 'document.save('addPicture.docx')' is used to create a newly edited docx file. You can add the image by using 'add_picture()' which contains the first parameter as 'cat-1.jpeg' is the path for the cat image. The widths and heights are the optional parameters that will be default as '72 dp,' but we have used 'Inches' for our purpose.

The above code gives the output as a newly created 'addedPicture.docx' file, which contains a cat image where there are 1.25 inches of width and height in the image.

Reading a Word Documents

You'll now read a sample word document from Python, and it can be found in:
Download Sample.

The first line in the code imports the Document from the 'docx' module, which is used to pass the required document file and to create an object .'obtainText' is a function that receives the file 'fullText.docx.' The looping is done for each paragraph, which is accessed by 'document.paragraphs' and is inserted into an empty list using the 'append' method. Finally, the function returns a list of paragraphs ending with a new line.

The above output gives the plain text without any style, color, etc. which is not a rich text document.

Congratulations

Congratulation, you've finished reading this tutorial.

If you would like to learn more about importing data in Python, try DataCamp's Introduction to Importing Data in Python course.

You can also look at the following resources to help broaden your knowledge on specific topics.

  1. Documentation of Python Docx
  2. Documentation of PyPDF2

References:
Automate Boring Stuff With Python: Working with Word Documents