Home > OS >  i tried a lot of things but nothin is workin. I try to merge mutiple pdf-files to one but i cant see
i tried a lot of things but nothin is workin. I try to merge mutiple pdf-files to one but i cant see

Time:06-29

from tkinter import filedialog as fd

import tkinter as tk

from PyPDF2 import PdfFileReader, PdfFileWriter, PdfFileMerger

import os

mother = tk.Tk()

base_pdf = fd.askopenfilename(filetypes=[('PDF files', '.pdf')], title='Wählen Sie bitte die Basis-PDF-Datei aus')

part_pdf = fd.askopenfilenames(title='Wählen Sie bitte die zugehörigen PDF-Dateien aus')

pdfs = [base_pdf, part_pdf]

x = os.path.basename(base_pdf)

y = x.split('.')[0]


for pdf in mother.splitlist(pdfs):

    pdf_writer = PdfFileWriter
    pdf_reader = PdfFileReader(pdf)
    for page in range(pdf_reader.getNumPages()):
         pdf_writer.addPage(pdf_reader.getPage(page))

with open(y   '_Beipack.pdf', 'wb') as target:

        pdf_writer = PdfFileWriter()
        pdf_writer.write(target)
        print('My PDF Merger', 'Glückwunsch, die PDF-Datei wurde erfolgreich erstellt!')

The TypeError i get is a missing argument: 'page'

CodePudding user response:

There are few issues in your code:

  • either use fd.askopenfilename() instead of fd.askopenfilenames() when selecting part_pdf or change pdfs = [base_pdf, part_pdf] to pdfs = [base_pdf, *part_pdf]
  • should create pdf_writer before the for loop
  • should not create pdf_writer again inside with block

Below is the modified code:

import tkinter as tk
from tkinter import filedialog as fd
from PyPDF2 import PdfFileReader, PdfFileWriter, PdfFileMerger
import os

mother = tk.Tk()

base_pdf = fd.askopenfilename(filetypes=[('PDF files', '.pdf')], title='Wählen Sie bitte die Basis-PDF-Datei aus')

part_pdf = fd.askopenfilenames(filetypes=[('PDF files', '.pdf')], title='Wählen Sie bitte die zugehörigen PDF-Dateien aus')

pdfs = [base_pdf, *part_pdf]

x = os.path.basename(base_pdf)

y = x.split('.')[0]

# create the pdf_writer
pdf_writer = PdfFileWriter()

for pdf in mother.splitlist(pdfs):
    pdf_reader = PdfFileReader(pdf)
    for page in range(pdf_reader.getNumPages()):
        pdf_writer.addPage(pdf_reader.getPage(page))

with open(y   '_Beipack.pdf', 'wb') as target:
    # removed: pdf_writer = PdfFileWriter()
    pdf_writer.write(target)
    print('My PDF Merger', 'Glückwunsch, die PDF-Datei wurde erfolgreich erstellt!')
  • Related