In this post we will review how to split and merge PDFs files.
Why is this required? Sometimes, to fill a form, you edit a PDF file in an online PDF editor site, but don't want to enter your credit card ID or bank account number in this online anonymous site.
How to do it anyway?
Fill all the form except the details you want to keep for yourself.
Split the PDF file to multiple pages using the following python code.
import os
from PyPDF2 import PdfReader, PdfWriter
def split_pdfs(input_file_path):
inputpdf = PdfReader(open(input_file_path, "rb"))
out_paths = []
if not os.path.exists("outputs"):
os.makedirs("outputs")
for i, page in enumerate(inputpdf.pages):
output = PdfWriter()
output.add_page(page)
out_file_path = f"outputs/{i}.pdf"
with open(out_file_path, "wb") as output_stream:
output.write(output_stream)
out_paths.append(out_file_path)
return out_paths
split_pdfs("document.pdf")
Next open the PDF file with the related page you want to fill offline, and update it using a image editor: make a screenshot, paste in any local image editor application, and add the text you need. Then, paste the image to your google drive as a new document, and download it as PDF.
Replace the page file that you've updated, and use the following python code to merge the pages back.
from PyPDF2 import PdfMerger
merger = PdfMerger()
for i in range(3):
merger.append('outputs/{}.pdf'.format(i))
merger.write("result.pdf")
merger.close()
Done! Your secrets are save...
No comments:
Post a Comment