Getting error when run python code

@kowts @Allan_Zimmermann .
I am run this code in open rpa
import os
from PyPDF2 import PdfReader, PdfWriter

Set the input file name

input_file = ‘Sample 3.pdf’

folder_Name = os.path.splitext(input_file)[0]

Set variable that the number of pages per output PDF file

pages_per_file = 1

Create the output folder as that of pdf name

if not os.path.exists(folder_Name):
os.mkdir(folder_Name)

now open the respective pdf fil and read it after reading store it in another variable.

with open(input_file, ‘rb’) as infile:
pdf_reader = PdfReader(infile)

# using for loop Iterate through the pages of the input PDF file
for page_num in range(len(pdf_reader.pages)):
    
    # It Create a new PDF file for every 'pages_per_file' number of pages
    if page_num % pages_per_file == 0:
        
        # If this is not the first file, close the previous one
        if 'outfile' in locals():
            outfile.close()
            
        # Create a new output PDF file
        output_file = folder_Name+'/{}_{}.pdf'.format(os.path.splitext(input_file)[0], page_num // pages_per_file + 1)
        outfile = open(output_file, 'wb')
        
        # Create a new PDF writer object for the output file
        pdf_writer = PdfWriter()

    # Get the current page of the input PDF file
    page = pdf_reader.pages[page_num]
    # Add the page to the current output PDF file
    pdf_writer.add_page(page)

    # If we have reached the end of a 'pages_per_file' number of pages, save the output file and start a new one
    if (page_num + 1) % pages_per_file == 0:
        pdf_writer.write(outfile)

# If there are any remaining pages, save them to a new output file
if 'outfile' in locals():
    pdf_writer.write(outfile)
    outfile.close()

But get the error like this :- ERROR: Could not find a version that satisfies the requirement PdfWriter (from versions: none)
ERROR: No matching distribution found for PdfWriter

i also use PIP install activity and pass the value like this {“PyPDF2”,“PdfReader”,“PdfWriter”,“os”}.
Correct me if am wrong

@kowts @Allan_Zimmermann Help me to sort from this problem

@Vivek_Sharma
The error message “No matching distribution found for PdfWriter” is due to trying to install “PdfWriter” separately using pip. “PdfWriter” is actually a part of the “PyPDF2” library, not a separate package. That’s why pip can’t find a version of “PdfWriter” to install.

“PdfReader”, “PdfWriter”, and “os” are not separate packages that need to be installed. Instead, “PdfReader” and “PdfWriter” are classes within the “PyPDF2” library, and “os” is a built-in Python module (already installed).

If you’re using pip to install the required packages, you only need to install “PyPDF2”. This can be done using the command:

pip install PyPDF2

UPDATE:
Based on the updated PyPDF2 documentation, the correct usage is PdfReader and PdfWriter classes. 1 2

2 Likes

@kowts
Thanks for your time man. It’s working fine

1 Like

@kowts I have one more question related to this…

@Vivek_Sharma, go ahead and ask your question! I’ll do my best to help you out.

1 Like

@kowts Sorry for asking this . But if i use this code in other rpa tools then how can i do it. Like if i have to use this code in uipath. Then how it is possible

Maybe it’s possible, but I’ve never tried it. I suggest asking in the tool forum where you intend to test the code. They can better provide you assistance.

1 Like

Okeh… Thanks i try it

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.