- Very simple example code
- ref : https://pypdf2.readthedocs.io/en/3.0.0/
* install
>> pip install PyPDF2
*sample src: sample pdf has 38 pages.
12 files are generated and each file has three pages.
this code uses fixed file count number because I already know page count,
but you can use "len(src_pdf.pages)" to get page count of source file.
from PyPDF2 import PdfFileReader, PdfFileWriter
src_pdf = PdfFileReader(open("./src.pdf'))
for file_index in range(12): # <= len(src_pdf.pages)/3
writer = PdfFileWriter()
for page_index in range(3):
writer.addPage(src_pdf.getPage(file_index*3+page_index+2))
writer.write(open("./out-{:02}.pdf".format(file_index), 'wb'))
== ver 3.0.1
generate single page
from PyPDF2 import PdfReader, PdfWriter
src_pdf = PdfReader("./multi-page.pdf")
number_of_pages = len(src_pdf.pages)
writer = PdfWriter()
for idx in range(number_of_pages):
writer.add_page(src_pdf.pages[idx])
writer.write(open(f"./single-page-{idx}.pdf", 'wb'))
댓글 없음:
댓글 쓰기