O'Reilly logo

Automate it! - Recipes to upskill your business by Chetan Giridhar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Manipulating PDFs (adding header/footer, merge, split, delete)

Ever wondered if you could merge PDF files programatically in a few seconds? Or could update header and footer of many PDF files in a jiffy? In this recipe, lets move on to do some interesting and most frequently performed operations on PDF files in this recipe.

Getting ready

For this recipe, we will use the PyPDF2 and fpdf modules that were installed for the earlier recipes.

How to do it...

  1. Let's start by working with the PdfFileMerge class of the PyPDF2. We use this class to merge multiple PDF files. The following code example does exactly the same:
     from PyPDF2 import PdfFileReader, PdfFileMerger import os merger = PdfFileMerger() files = [x for x in os.listdir('.') if x.endswith('.pdf')] ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required