Pdf splitting/stacking #170
-
Is it possible to split pdf per pages or stack several pdf files to one? |
Beta Was this translation helpful? Give feedback.
Answered by
mara004
Jan 28, 2023
Replies: 1 comment 4 replies
-
Yes. You can create new pdfs and import pages from foreign pdfs. Thereby you can split/merge pdfs and move pages programmatically. However, pypdfium2/src/pypdfium2/_helpers/document.py Lines 366 to 394 in 5c05d6f So basically what you would do is something like this # example: split a 6-page document after page 3
import pypdfium2 as pdfium
src_pdf = pdfium.PdfDocument("/abspath/to/source.pdf")
out_a = pdfium.PdfDocument.new()
out_b = pdfium.PdfDocument.new()
out_a.import_pages(src_pdf, [0, 1, 2])
out_b.import_pages(src_pdf, [3, 4, 5])
# (note: saving to paths directly needs devel-new as well, main only accepts buffers)
out_a.save("/abspath/to/out_a.pdf")
out_b.save("/abspath/to/out_b.pdf") |
Beta Was this translation helpful? Give feedback.
4 replies
Answer selected by
PasaOpasen
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yes. You can create new pdfs and import pages from foreign pdfs. Thereby you can split/merge pdfs and move pages programmatically.
However,
main
doesn't have a helper method for that yet, so I suggest that you take a look at thedevel-new
branch, which providesPdfDocument.import_pages()
:pypdfium2/src/pypdfium2/_helpers/document.py
Lines 366 to 394 in 5c05d6f