コード例 #1
0
def PdfMultiplePassword(filepaths, password):
    # Check if files exists
    check_path = [os.path.isfile(x) for x in filepaths]

    # Gets the files extension
    file_extensions = [os.path.splitext(x)[1] for x in filepaths]

    # Check if files extension are pdf
    file_extensions_check = [x for x in file_extensions if x != ".pdf"]

    if False in check_path:

        # Get the index of the file that doesn't exists
        index = check_path.index(False)
        print(f"File Doesn't Exists: {filepaths[index]}")
        sys.exit()

    else:
        # Not a PDF file is given
        if file_extensions_check:
            print("Submit Only PDF Files")
            sys.exit()

        else:
            count = 1
            # Iterate through every pdf of the filepaths
            for path in filepaths:

                # Create a PdfFileWriter object
                pdf_writer = PdfFileWriter()

                # Open our PDF file with the PdfFileReader
                pdf_reader = PdfFileReader(path)

                # Get the page at index idx
                for page in range(pdf_reader.getNumPages()):
                    # Add each page to the writer object
                    pdf_writer.addPage(pdf_reader.getPage(page))

                # The output filename
                output_file = f"merge_enc_{count}_{ts}.pdf"

                # Encrypt the new file with the entered password
                pdf_writer.encrypt(password, use_128bit=True)

                # Write out the merged PDF
                with open(output_file, 'wb') as file:
                    pdf_writer.write(file)

                count += 1
                print('File Written To Path:', output_file)
コード例 #2
0
def PdfPassword(filepath, password):
    # Check if file exists
    checkFile = os.path.isfile(filepath)

    if checkFile:
        # Get the path of directory and filename
        path, filename = os.path.split(filepath)

        # Get the file extension to check for pdf files
        file_extension = os.path.splitext(filepath)[1]

        if file_extension == ".pdf":

            # The output filename
            output_file = os.path.join(path, f"temp_{ts}_{filename}")

            # Create a PdfFileWriter object
            pdf_writer = PdfFileWriter()

            # Open our PDF file with the PdfFileReader
            file = PdfFileReader(filepath)

            # Get number of pages in original file
            # Iterate through every page of the original file and add it to our new file
            for idx in range(file.numPages):
                # Get the page at index idx
                page = file.getPage(idx)

                # Add it to the output file
                pdf_writer.addPage(page)

            # Encrypt the new file with the entered password
            pdf_writer.encrypt(password, use_128bit=True)

            # Open a new file
            with open(output_file, "wb") as file:
                # Write our encrypted PDF to this file
                pdf_writer.write(file)

            print('File Written To Path:', output_file)

        else:
            # File extension is not PDF
            print(
                f"Not A PDF File Given, File Has Extension: {file_extension}")
            sys.exit()

    else:
        # No file exists on the current path
        print("Check The File Path")
        sys.exit()