

Open both PDF files in read binary mode and store the two resulting File objects in pdf1File and pdf2File. ❽ > pdfOutputFile = open('combinedminutes.pdf', 'wb') > for pageNum in range(pdf2Reader.numPages): > for pageNum in range(pdf1Reader.numPages):

❷ > pdf2Reader = PyPDF2.PdfFileReader(pdf2File) ❶ > pdf1Reader = PyPDF2.PdfFileReader(pdf1File) > pdf2File = open('meetingminutes2.pdf', 'rb') > pdf1File = open('meetingminutes.pdf', 'rb') Enter the following into the interactive shell:

This allows you to combine multiple PDF files, cut unwanted pages, or reorder pages.ĭownload meetingminutes.pdf and meetingminutes2.pdf from and place the PDFs in the current working directory. You can use PyPDF2 to copy pages from one PDF document to another. Your program will have to call decrypt() again the next time it is run. After your program terminates, the file on your hard drive remains encrypted. Note that the decrypt() method decrypts only the PdfFileReader object, not the actual PDF file. If given the wrong password, the decrypt() function will return 0 and getPage() will continue to fail. After you call decrypt() with the correct password, you’ll see that calling getPage() no longer causes an error.
#PDF TO TEXT DOCUMENT PASSWORD#
To read an encrypted PDF, call the decrypt() function and pass the password as a string ❸. Any attempt to call a function that reads the file before it has been decrypted with the correct password will result in an error ❷. : file has not been decryptedĪll PdfFileReader objects have an isEncrypted attribute that is True if the PDF is encrypted and False if it isn’t ❶. Raise utils.PdfReadError("file has not been decrypted") > pdfReader = PyPDF2.PdfFileReader(open('encrypted.pdf', 'rb'))įile "C:\Python34\lib\site-packages\PyPDF2\pdf.py", line 1173, in getObject Enter the following into the interactive shell with the PDF you downloaded, which has been encrypted with the password rosebud: Some PDF documents have an encryption feature that will keep them from being read until whoever is opening the document provides a password. Still, this approximation of the PDF text content may be good enough for your program. “Chas” Roemer, President from the PDF is absent from the string returned by extractText(), and the spacing is sometimes off. The text extraction isn’t perfect: The text Charles E. Once you have your Page object, call its extractText() method to return a string of the page’s text ❸. To get the first page of this document, you would want to call pdfReader.getPage(0), not getPage(42) or getPage(1). For example, say your PDF is a three-page excerpt from a longer report, and its pages are numbered 42, 43, and 44. This is always the case, even if pages are numbered differently within the document. PyPDF2 uses a zero-based index for getting pages: The first page is page 0, the second is Introduction, and so on. You can get a Page object by calling the getPage() method ❷ on a PdfFileReader object and passing it the page number of the page you’re interested in-in our case, 0. To extract text from a page, you need to get a Page object, which represents a single page of a PDF, from a PdfFileReader object.

The example PDF has 19 pages, but let’s extract text from only the first page. The total number of pages in the document is stored in the numPages attribute of a PdfFileReader object ❶. Store this PdfFileReader object in pdfReader. To get a PdfFileReader object that represents this PDF, call PyPDF2.PdfFileReader() and pass it pdfFileObj. Then open meetingminutes.pdf in read binary mode and store it in pdfFileObj. BOARD of ELEMENTARY and SECONDARY EDUCATION 'įirst, import the PyPDF2 module. \n The Board of Elementary and Secondary Education shall provide leadershipĪnd create policies for education that expand opportunities for children,Įmpower families and communities, and advance Louisiana in an increasinglyĬompetitive global market. 'OOFFFFIICCIIAALL BBOOAARRDD MMIINNUUTTEESS Meeting of March 7, 2015 > pdfReader = PyPDF2.PdfFileReader(pdfFileObj) > pdfFileObj = open('meetingminutes.pdf', 'rb') Figure 13-1. The PDF page that we will be extracting text fromĭownload this PDF from, and enter the following into the interactive shell:
