def rev_file(file_path, draw, spec): pdf = Merger() if draw in draws: draw_pdf = Reader(draws.get(draw, draw)) if spec: spec_pdf = Reader(spec if '.' in spec[-3:] else get_spec(spec)) if draw and spec: pdf.append(draw_pdf, pages=PageRange(':' if draw in draws else ':-' + spec_pdf.numPages)) pdf.append(spec_pdf, pages=PageRange(':' if spec.count('.') == 1 else draw_pdf.numPages + ':')) else: pdf.append(spec_pdf if spec else draw_pdf) with open(file_path, 'wb') as f: pdf.write(f)
def custom_range(self, r): total_ranges = [x for x in r.split(",")] for current_range in total_ranges: custom_range = [int(x) - 1 for x in current_range.split("-")] count = 0 for _ in range(custom_range[1] + 1 - custom_range[0]): count += 1 print( f"-> [OK] Página {custom_range[0]+count} dividida com sucesso..." ) merger = PdfFileMerger(strict=False) merger.append( self.src_file.open(mode="rb"), pages=PageRange( f"{str(custom_range[0])}:{str(custom_range[1]+1)}")) new_file = pathlib.Path( self.dst_path / f"{self.src_file.stem}-pags-{custom_range[0]+1}-{custom_range[1]+1}.pdf" ) with new_file.open(mode="wb") as f: merger.write(f) print("") print( f"-> [!] {custom_range[1]+1 - custom_range[0]} páginas divididas com sucesso..." ) print(f"-> [+] Novo arquivo salvo em: {new_file.parents[0]}") print("")
def merge_partials(path, partials): """Merge partial pdfs in one single PDF""" merger = PdfFileMerger() for idx, partial in enumerate(partials): # Gallica appends 2 pages to each pdf fetched so we don't write those # pagse except for the first partial opts = {"pages": PageRange('2:')} if idx else {} merger.append(partial, **opts) merger.write(path) merger.close()
def pdf_merge(pdf_files, output_file, pages): print('creating {} ...'.format(output_file.name)) merger = PdfFileMerger() for i, f in enumerate(pdf_files): if i < len(pages): print(' {}: merging in pages -> {}'.format(f.name, pages[i])) # directly use PageRange specification merger.append(fileobj=f, pages=PageRange(pages[i])) else: print(' {}: merging all pages'.format(f.name)) merger.append(fileobj=f) print('writing {} ...'.format(output_file.name)) merger.write(output_file)
def each_page(self): reader = PdfFileReader(self.src_file.open(mode="rb"), strict=False) for page in range(reader.numPages): merger = PdfFileMerger(strict=False) merger.append(self.src_file.open(mode="rb"), pages=PageRange(str(page))) print(f"-> [OK] Página {page+1} dividida com sucesso...") new_file = pathlib.Path(self.dst_path / f"{self.src_file.stem}-pag-{page+1}.pdf") with new_file.open(mode="wb") as f: merger.write(f) print("") print(f"-> [!] {reader.numPages} páginas divididas com sucesso...") print(f"-> [+] Novos arquivos salvos em: {new_file.parents[0]}")
def custom_pages(self, p): custom_pages = [int(x) for x in p.split("-")] for page in custom_pages: merger = PdfFileMerger(strict=False) merger.append(self.src_file.open(mode="rb"), pages=PageRange(str(page - 1))) print(f"-> [OK] Página {page} adicionada com sucesso...") new_file = pathlib.Path(self.dst_path / f"{self.src_file.stem}-pag-{page}.pdf") with new_file.open(mode="wb") as f: merger.write(f) print("") print(f"-> [!] {len(custom_pages)} páginas divididas com sucesso...") print(f"-> [+] Novos arquivos salvos em: {new_file.parents[0]}")
from PyPDF2 import PdfFileMerger, PageRange import os import re #pdfs = ['file1.pdf', 'file2.pdf', 'file3.pdf', 'file4.pdf'] merger = PdfFileMerger() cwd = os.getcwd() files = os.listdir(cwd) pdffiles = [f for f in files if f.endswith((".pdf"))] pdffiles.sort(key=lambda f: int(re.sub('\D', '', f))) for pdffile in pdffiles: print(pdffile) merger.append(pdffile, pages=PageRange('1:-1')) merger.write("result.pdf") merger.close()