Split PDF to single pages with simple Python script

Published: 09 August 2022
on channel: Rafal Kobus
432
8

Rafal Kobus

  / rafa%c5%82-kobus-b6051314b  

from PyPDF2 import PdfFileReader, PdfFileWriter
import os
import sys
import glob
abspath = os.path.abspath(__file__)
dname = os.path.dirname(abspath)
os.chdir(dname)

if getattr(sys, 'frozen', False):
_location_ = os.path.dirname(os.path.realpath(sys.executable))
elif __file__:
_location_ = os.path.realpath(
os.path.join(os.getcwd(), os.path.dirname(__file__)))

for file in glob.glob(__location__ + "/*.pdf"):
if file.endswith('.pdf'):
pdf_file = open(os.path.join(__location__, file), 'rb')
pdf_reader = PdfFileReader(pdf_file)

pageNumbers = pdf_reader.getNumPages()

for i in range (pageNumbers):
pdf_writer = PdfFileWriter()
pdf_writer.addPage(pdf_reader.getPage(i))
split_motive = open('Page ' + str(i+1) + '.pdf', 'wb')
pdf_writer.write(split_motive)
split_motive.close()

pdf_file.close()

#PyPDF2 #PDF #Python


On this page of the site you can watch the video online Split PDF to single pages with simple Python script with a duration of hours minute second in good quality, which was uploaded by the user Rafal Kobus 09 August 2022, share the link with friends and acquaintances, this video has already been watched 432 times on youtube and it was liked by 8 viewers. Enjoy your viewing!