How to replace/delete text from a pdf using python?

Viewed 15543

I have code that hides parts of the pdf (by just covering it with a white polygon) but the issue with this is, the text is still there, if you ctrl-f you can still find it.

My goal is to actually remove the text from the pdf itself. Using pdfminer I managed to extract the text from the pdf but I don't know if its possible to actually "replace" the text with say just some empty spaces. Is such a thing possible using python? Extracting it isn't enough. I need the text to be removed from the PDF

4 Answers

Is such a thing possible? Yes, although it is not recommended. In my opinion, your best bet is to open and read your existing file, move it to an editable format, remove whatever text that you don't want present and then convert it back.

However, you could extract the data and remove it from memory by using:

import PyPDF2 

# creating a pdf file object 
pdfFileObj = open('example.pdf', 'rb') 

# creating a pdf reader object 
pdfReader = PyPDF2.PdfFileReader(pdfFileObj) 

# printing number of pages in pdf file 
print(pdfReader.numPages) 

# creating a page object 
pageObj = pdfReader.getPage(0) 

# extracting text from page 
print(pageObj.extractText()) 

# closing the pdf file object 
pdfFileObj.close() 

Line by line, this program would:

pdfFileObj = open('example.pdf', 'rb') Open the example.pdf and save the file object as pdfFileObj.

pdfReader = PyPDF2.PdfFileReader(pdfFileObj) Create an object of PdfFileReader and pass the PDF file object whole getting a PDF reader object.

print(pdfReader.numPages) Give the number of pages.

pageObj = pdfReader.getPage(0) Create an object of PageObject class. PDF reader object has function getPage() which takes page number (starting form index 0) as an argument and returns the page object.

print(pageObj.extractText()) Extract text from the PDF page.

pdfFileObj.close() Close the PDF file object.

The replacement text would simply be "", as you want to remove all instances / cases of a certain piece of text.

I used pdf-redactor in one of my projects and it works pretty nice.

Here is an example how to redact Social Security Numbers from text layer.

This is kind of memory intensive but you can copy the rest of the pdf apart from the part you are removing and then overwrite the file with the new version which does not contain the part you wish to remove. You can do this using PyPDF by retrieving a content stream and finding and removing the relevant parts.

PyPDF docs https://pythonhosted.org/PyPDF2/PageObject.html?highlight=getcontents#PyPDF2.pdf.PageObject.getContents;

PDF standard https://www.adobe.com/content/dam/acom/en/devnet/pdf/pdfs/PDF32000_2008.pdf pg 78, pg 81;

I know I am late but for future readers here is a workaround I found to resolve this using pymupdf. This solution successfully deletes text from pdf.

page = doc.load_page(0)

draft = page.search_for("Invoice")

for rect in draft:
    annot = page.add_redact_annot(rect)
    page.apply_redactions()
    page.apply_redactions(images=fitz.PDF_REDACT_IMAGE_NONE)
# then save the doc to a new PDF:
doc.save("new.pdf", garbage=3, deflate=True)
Related