If you run the above code and want to see what page_one_text variable holds, you will find the following output. Here the parameter 0 indicates the first page of the pdf page_one = pdf_reader.getPage(0) page_one_text = page_one.extractText() #Finally the extractText() extracts the the texts in a text format of page 1. #pdfFileReader() reads the text form the pdf pdf_reader = PyPDF2.PdfFileReader(f) #the following lines of code will output the number of pages of the pdf pdf_reader.numPages #getPage()reads the text of a specific page. The following piece of codes will help us to read form the pages of pdf. It’s time to read the text form the page. You may download the US_Declaration.pdf file from the here. Here, the file ‘US_Declaration.pdf’ is located in the same directory of the jupyter notebook file location. Read more why ‘rb’ is used instead of ‘r’ # Notice we read it as a binary with 'rb' f = open('US_Declaration.pdf','rb') Notice how we use the binary method of reading, ‘rb’, instead of just ‘r’. Now, we open a pdf, then create a reader object for it. There are many parameters to consider in this aspect.Īs far as PyPDF2 is concerned, it can only read the text from a PDF document, it won’t be able to grab images or other media files from a PDF.įirst of all need to import the library PyPDF2 as follows # note the capitalization import PyPDF2 The reason for this is because of the many different parameters for a PDF and how non-standard the settings can be, text could be shown as an image instead of a utf-8 encoding. If you find yourself in this situation, try using the libraries linked above, but keep in mind, these may also not work. PDFs that are too blurry, have a special encoding, encrypted, or maybe just created with a particular program that doesn’t work well with PyPDF2 won’t be able to be read. Keep in mind that not every PDF file can be read with this library. You can install it with (note the case-sensitivity, you need to make sure your capitalization matches): pip install PyPDF2 There are many libraries in Python for working with PDFs, each with their pros and cons, the most common one being PyPDF2. Often you will have to deal with PDF files. The following part is all about the PyPDF2 library of python for working with PDF files.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |