PyPDF2 para manipular arquivos PDF (PdfMerger)

This commit is contained in:
Luiz Otávio
2023-02-18 10:44:33 -03:00
parent 1c8bb20374
commit 2c8a030bac
2 changed files with 16 additions and 2 deletions

Binary file not shown.

View File

@@ -1,4 +1,4 @@
# PyPDF2 para manipular arquivos PDF (PdfWriter)
# PyPDF2 para manipular arquivos PDF (PdfMerger)
# PyPDF2 é uma biblioteca de manipulação de arquivos PDF feita em Python puro,
# gratuita e de código aberto. Ela é capaz de ler, manipular, escrever e unir
# dados de arquivos PDF, assim como adicionar anotações, transformar páginas,
@@ -9,7 +9,7 @@
# pip install pypdf2
from pathlib import Path
from PyPDF2 import PdfReader, PdfWriter
from PyPDF2 import PdfMerger, PdfReader, PdfWriter
PASTA_RAIZ = Path(__file__).parent
PASTA_ORIGINAIS = PASTA_RAIZ / 'pdfs_originais'
@@ -39,3 +39,17 @@ for i, page in enumerate(reader.pages):
with open(PASTA_NOVA / f'page{i}.pdf', 'wb') as arquivo:
writer.add_page(page)
writer.write(arquivo) # type: ignore
files = [
PASTA_NOVA / 'page1.pdf',
PASTA_NOVA / 'page0.pdf',
]
merger = PdfMerger()
for file in files:
merger.append(file) # type: ignore
merger.write(PASTA_NOVA / 'MERGED.pdf') # type: ignore
merger.close()