How to run python script through UIPath

Hi Team,

Please let me know how to run below python script through UiPath,

import os

from PyPDF2 import PdfReader, PdfWriter

def write_pdf_writer_to_file(pdf_writer, base_filename, counter):

os.makedirs(output_folder, exist_ok=True)

output_filename = os.path.join('output', f'{base_filename}_part_{counter}.pdf')

with open(output_filename, 'wb') as out:

    pdf_writer.write(out)

print('Created:', output_filename)

return output_filename

def pdf_splitter(path, size_limit_mb=32):

fname = os.path.splitext(os.path.basename(path))[0]

pdf = PdfReader(path)

counter = 1

pages_to_add = []

for page in pdf.pages:

    pages_to_add.append(page)

    pdf_writer = PdfWriter()

    for page_to_add in pages_to_add:

        pdf_writer.add_page(page_to_add)

    temp_filename = write_pdf_writer_to_file(pdf_writer, fname, counter)

    

    if os.path.getsize(temp_filename) > size_limit_mb * 1024 * 1024:

        # If the file size exceeds the limit, remove the last page from the list and save the file

        pages_to_add.pop() # Remove the last page from the list

        if pages_to_add: # If there are still pages left

            # Write the current list of pages to a file, excluding the last one that caused overflow

            pdf_writer = PdfWriter()

            for page_to_add in pages_to_add:

                pdf_writer.add_page(page_to_add)

            write_pdf_writer_to_file(pdf_writer, fname, counter)

            counter += 1

        # Reset the list of pages to add with only the last page

        pages_to_add = [page]

# Save any remaining pages in the last PDF part

if pages_to_add:

    pdf_writer = PdfWriter()

    for page_to_add in pages_to_add:

        pdf_writer.add_page(page_to_add)

    write_pdf_writer_to_file(pdf_writer, fname, counter)

filename = “100MB-TESTFILE.ORG.pdf”

output_folder = “output/”

pdf_splitter(os.path.join(filename))

Hi @Smitesh_Aher2

First try to execute your script in the python environment, Install all the dependency then follow these steps.

Python Scope: Set the PythonPath to the path of your Python executable.
Load Python Script: Provide the path to your .py script.
Invoke Python Method: Call the pdf_splitter method and pass arguments such as the file path and size limit.

You can refer the below video for ref.

Hi @AJ_Ask,

Thanks for your response. I am able to run python script through cmd but, now i am facing issue in the invoke python method activity.
Can you please help me what values should i enter in the ‘Name’ & ‘Input Parameters’ fields of invoke python method activity?

In The Name of the method that you are calling & Input parameters are these as per your script

Provide the necessary inputs for the function:

  • path: The full path of the PDF file.
  • size_limit_mb: Optional size limit for the split parts (if different from default).
  • output_folder: Optional output folder path (if different from default).

Eg. InputParameters: { "C:\Files\100MB-TESTFILE.ORG.pdf", 32, "C:\Files\Output\" }

We are calling 2 methods (pdf_splitter & write_pdf_writer_to_file) in the script so, how to call both methods in the name field?

Change the code to call one method internally from another. Use chatgpt it will simplify your code. or Use 2 invoke method. I would just change the code,