web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / Split PDFs dynamically
Power Automate
Answered

Split PDFs dynamically

(0) ShareShare
ReportReport
Posted on by 24
I'm downloading a PDF from a Website and the PDF have always different number of Pages.
 
How can I split the PDF in single pages without knowing the total number of pages?
 
I need this to work on unattended fashion. I can't use dialog box to pass the file
I have the same question (0)
  • Verified answer
    VishnuReddy1997 Profile Picture
    2,656 Super User 2025 Season 2 on at
     
    Please find the solution.
     
     
    Python code: Please save the python code in notepad and save as .py file and provide it in Run Powershell script action.
    import fitz  # PyMuPDF
    import sys
    
    def count_pages(pdf_path):
        pdf_document = fitz.open(pdf_path)
        num_pages = pdf_document.page_count
        return num_pages
    
    if __name__ == "__main__":
        if len(sys.argv) != 2:
            print("Usage: python count_pages_pymupdf.py <pdf_path>")
            sys.exit(1)
    
        pdf_path = sys.argv[1]
        num_pages = count_pages(pdf_path)
        print(f"{num_pages}")
     
     
    Please copy paste the below script to your PAD and change the paths.
    Folder.GetFiles Folder: $'''C:\\Users\\Desktop\\Power Automate Desktop\\Practice\\PDF\\PAD PDF 0''' FileFilter: $'''*''' IncludeSubfolders: False FailOnAccessDenied: True SortBy1: Folder.SortBy.NoSort SortDescending1: False SortBy2: Folder.SortBy.NoSort SortDescending2: False SortBy3: Folder.SortBy.NoSort SortDescending3: False Files=> PDF_Files
    LOOP FOREACH CurrentItem IN PDF_Files
        # Script to Count Number of pages in PDF
        @@copilotGeneratedAction: 'False'
    Scripting.RunPowershellScript.RunPowershellScript Script: $'''Python \"C:\\Users\\Desktop\\PDFPagesCount.py\" \"%CurrentItem.FullName%\"''' ScriptOutput=> PDFPagesCount ScriptError=> ScriptError2
        Text.ToNumber Text: PDFPagesCount Number=> PDFPagesCount
        LOOP LoopIndex FROM 1 TO PDFPagesCount STEP 1
            Pdf.ExtractPages PDFFile: CurrentItem PageSelection: LoopIndex ExtractedPDFPath: $'''C:\\Users\\Desktop\\Power Automate Desktop\\Practice\\PDF\\PAD PDF 1\\%CurrentItem.NameWithoutExtension%.pdf''' IfFileExists: Pdf.IfFileExists.AddSequentialSuffix ExtractedPDFFile=> ExtractedPDF
        END
    END
    






    (Note:- if you got your solution you can mark as solution and gives kudos)
    ​​​​​​​
    Thanks & Regards
    Vishnu Red

  • Suggested answer
    eetuRobo Profile Picture
    4,204 Super User 2025 Season 2 on at
    @VishnuReddy1997's solution seems clean so mark that as solution if that does the job for you. 

    Alternative way to using scripts would be using Extract PDF file pages to new PDF file -action to go to "pages out of bounds error" and since the error message returns all the pages that were not in PDF that you gave it to extract then you can just take the first non existing page number from the error message and decrease it by 1 to get the last page number.

    Not sure if this is the best way to do things since there is a possibility that the error message could change in future updates. 


    Here is the flow:


    Explanation:
    1. Have Extract PDF file pages to new PDF file to take more pages than you know there will be (for example 1-99). Add On error on that action to continue to next action since it should always go to error.
    2. use Get last error -action to get the error message that you parse the last page from.
    Also: Consider adding an if condition that checks that its actually "Pages out of bounds " error and not "file not found" or something like that where the flow should stop since its an actual error.
     

    3. Then Replace the LastError.Message "Pages out of bounds. Page numbers '" with empty string. Then you have Replaced -variable with the first character being the first page that does not exist.
     



    4. Then take the first page that doesn't exist in a variable and convert it to number. Then decrease it by one to get the last page number. Then just loop through and use Extract PDF file pages to new PDF file -action to craete PDFs from each existing pdf page.
     
  • Verified answer
    Esbastian Profile Picture
    24 on at
    Thanks Vishnu!

    This works. I also found an other way

     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Automate

#1
Michael E. Gernaey Profile Picture

Michael E. Gernaey 525 Super User 2025 Season 2

#2
Tomac Profile Picture

Tomac 324 Moderator

#3
abm abm Profile Picture

abm abm 232 Most Valuable Professional

Last 30 days Overall leaderboard