title: “Concatenar PDFs” date: “2023-06-19”
Mesclando diversos diretórios com pdfs em um único arquivo pdf utilizando biblioteca glob do Python. Perguntei à IA, em fase experimental, e obtive:
To concatenate multiple PDF files into a single PDF file, you can use PyPDF2, a Python library for working with PDF files. Here's an example code that takes two PDF files and creates a merged PDF file:
import PyPDF2 pdf_files = [“file1.pdf”, “file2.pdf”] output_pdf = “merged.pdf”
pdf_writer = PyPDF2.PdfFileWriter()
for pdf_file in pdf_files: pdf_reader = PyPDF2.PdfFileReader(open(pdf_file, “rb”)) for page_num in range(pdf_reader.getNumPages()): page_obj = pdf_reader.getPage(page_num) pdf_writer.addPage(page_obj)
with open(output_pdf, “wb”) as f: pdf_writer.write(f)
You can modify this code to concatenate multiple PDF files or even entire directories full of PDF files by using
glob
to get all the files in a directory.Mas fiz assim:
https://gist.github.com/emanoelopes/59c5fc1a3ed2d961aabe1c9a357fc901
Emanoel Lopes