Skip to content

Instantly share code, notes, and snippets.

@walbon
Created March 23, 2023 19:18
Show Gist options
  • Save walbon/162f48100154616130235ed76c2d3723 to your computer and use it in GitHub Desktop.
Save walbon/162f48100154616130235ed76c2d3723 to your computer and use it in GitHub Desktop.
Scripting to remove part of image in PDF that belongs a text, just taking of the last 20 pixels of image.
import PyPDF2
import sys
import os
def remover(target, file_name):
print(f"!Removing assinature of \"{target}\" from {file_name}")
try:
pdf_file = open(file_name,'rb')
except:
print(f"{file_name} : Error to load it")
return
pdf_reader = PyPDF2.PdfReader(pdf_file)
pdf_writer = PyPDF2.PdfWriter()
for page_num in range(len(pdf_reader.pages)):
page = pdf_reader.pages[page_num]
if target in page.extract_text():
# Setting the cropbox for botton side of page with 20 of height
page.mediabox.lower_left = (0, 20)
# cropping the page based on the previous setting
page.cropbox.upper_right = (page.mediabox.left, page.mediabox.right)
pdf_writer.add_page(page)
with open("_"+file_name,'wb') as output_file:
pdf_writer.write(output_file)
print(f">>> DONE \"{file_name}\" save as \"_{file_name}\"")
#################################
print(f"Thank you for choice our services.")
target = None
for i in sys.argv[1:]:
if not os.path.isfile(i):
target = str(i)
if target is None:
print("Target string not defined")
else:
for item in sys.argv[1:]:
print(80*"-")
if os.path.isfile(item):
remover(target, item)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment