Merging PDF
#!pip install PyPDF2
import time
import os
from google.colab import drive
from google.colab import files
from google.colab import auth
drive.mount('/content/drive', force_remount=False)
import PyPDF2
pdf_dir = '/content/drive/My Drive/pdf/'
pdf_files = [filename for filename in os.listdir(pdf_dir) if filename.endswith('.pdf')]
print(pdf_files)
merger = PyPDF2.PdfMerger()
for filename in pdf_files:
file_path = os.path.join(pdf_dir, filename)
merger.append(file_path)
output_path = '/content/drive/My Drive/pdf/merged.pdf'
with open(output_path, 'wb') as output_file:
merger.write(output_file)
=================================================================================
Unlocking PDF
import PyPDF2
import time
import os
from google.colab import drive
from google.colab import files
from google.colab import auth
drive.mount('/content/drive', force_remount=False)
pdf_file = open('/content/drive/MyDrive/pdf/1.pdf', 'rb')
# create a PDF object
pdf_reader = PyPDF2.PdfReader(pdf_file)
# check if the PDF file is encrypted or not
if pdf_reader.is_encrypted:
# enter the password to unlock the PDF file
password = input("Enter the password: ")
pdf_reader.decrypt(password)
# create a new PDF writer object
pdf_writer = PyPDF2.PdfWriter()
# loop through all the pages of PDF and add them to the PDF writer object
for page_num in range(len(pdf_reader.pages)):
page_obj = pdf_reader.pages[page_num]
pdf_writer.add_page(page_obj)
output_filename = '/content/drive/MyDrive/pdf/unlocked_pdf_file.pdf'
# write the output file without any password protection
with open(output_filename, 'wb') as output:
pdf_writer.write(output)
print("Password removed successfully!")