2

im having an issue with this line:

pdf_writer = PyPDF2.PdfFileWriter(strict=False)

it return this:

Multiple definitions in dictionary at byte 0x1fd19 for key /Info Multiple definitions in dictionary at byte 0x1fd25 for key /Info
Multiple definitions in dictionary at byte 0x1fd31 for key /Info
Multiple definitions in dictionary at byte 0x207d6 for key /Info
Multiple definitions in dictionary at byte 0x207e2 for key /Info
Multiple definitions in dictionary at byte 0x207ee for key /Info
/Prev=0 in the trailer - assuming there is no previous xref table /Prev=0 in the trailer - assuming there is no previous xref table
Multiple definitions in dictionary at byte 0x23f8a for key /Info Multiple definitions in dictionary at byte 0x23f96 for key /Info
Multiple definitions in dictionary at byte 0x23fa2 for key /Info
Multiple definitions in dictionary at byte 0x1e634 for key /Info
Multiple definitions in dictionary at byte 0x1e640 for key /Info
Multiple definitions in dictionary at byte 0x1e64c for key /Info
/Prev=0 in the trailer - assuming there is no previous xref table /Prev=0 in the trailer - assuming there is no previous xref table
/Prev=0 in the trailer - assuming there is no previous xref table /Prev=0 in the trailer - assuming there is no previous xref table

does someone have an idea of why is this happening?

code:

import os
import PyPDF2
import re
import math

def main():
    # Set the directory containing the PDF files
    inputDirectory = 'input_path'
    # Set the directory where the output PDF files will be saved
    outputDirectory = 'output_path'

    # Get a list of all of the PDF filenames in the input directory
    pdfFilenames = [f for f in os.listdir(inputDirectory) if f.endswith('.pdf')]

    pdf_dict = {}
    for file in pdfFilenames:
        if file.endswith('R.pdf'):
            pdf_dict[file] = file.replace('R.pdf', 'V.pdf')
        elif file.endswith('V.pdf'):
            pdf_dict[file.replace('V.pdf', 'R.pdf')] = file
        

    err = []
    l_fin = []
    fileAct = 0
    print("Début d'execution")
    for fileR, fileV in pdf_dict.items():
        # Ouvrez les fichiers PDF
        with open('CNI-R-V//' + fileR, 'rb') as file_handle_1, open('CNI-R-V//' + fileV, 'rb') as file_handle_2:
            # Créez des objets PDF à partir des fichiers
            try:
                pdf1 = PyPDF2.PdfFileReader(file_handle_1, strict=False)
                pdf2 = PyPDF2.PdfFileReader(file_handle_2, strict=False)
                # Créez un nouvel objet PDF
                pdf_writer = PyPDF2.PdfFileWriter(strict=False)
                # Ajoutez chaque page des fichiers PDF originaux au nouveau fichier
                for page_num in range(pdf1.getNumPages()):
                    pdf_writer.addPage(pdf1.getPage(page_num))
                for page_num in range(pdf2.getNumPages()):
                    pdf_writer.addPage(pdf2.getPage(page_num))
                # Créez le fichier PDF combiné en utilisant le nom des fichiers originaux
                with open(f'output/{fileR[:-5]}_combined.pdf', 'wb') as fh:
                    pdf_writer.write(fh)
                progressBar(len(pdf_dict), fileAct, err)
                fileAct += 1
            except:
                #print('erreur', fileR, fileV)
                err.append((fileR, fileV))
                progressBar(len(pdf_dict), fileAct, err)
                fileAct += 1
                continue       
        

def progressBar(nbVal, actVal, err):
  percent = actVal/nbVal
  nEquals = math.floor(percent*20)
  bar = '=' * nEquals + ' ' * ( 20 - nEquals )

  print("\r", end='')
  print(f"[{bar}] {percent*100:.2f}%", end='')

  if percent == 1:
    print("\nFin d'execution")
    if err != []:
      print("Erreurs:")
      for el in err:
        print(el)

if __name__ == '__main__':
    main()

My code should create a pdf from 2 other pdfs. but the line that is technically able to write a pdf doesn't work

VullWen
  • 19
  • 1

0 Answers0