Skip to main content

Notifications

Community site session details

Community site session details

Session Id : 6BGDk7/8p26Hw3bIK0HvIc
Power Automate - Power Automate Desktop
Suggested answer

how to get the total number pages from a pdf file

Like (1) ShareShare
ReportReport
Posted on 23 Sep 2024 13:05:17 by 4
Hello All,

i am trying to fetch the total no.of pages from a pdf file in power automate desktop. so,is there any way to fetch it?

thanks in advance.

 
Categories:
  • Suggested answer
    VishnuReddy1997 Profile Picture
    2,324 Super User 2025 Season 1 on 24 Sep 2024 at 08:11:04
    how to get the total number pages from a pdf file
    Hey Hi,
     
    If the pages count is too high,it will be a time take process @deenuji has provided.
     
    It will be faster and easiest process if you try with Python code and you can invoke the Python code as below shown in PAD.
     
     
    Python Code :
    Please save this python code as .py file.
     
    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}")
     
     
    (Note:- if you got your solution you can mark as solution and gives kudos)
     
    Thanks & Regards
    Vishnu Reddy
  • Suggested answer
    Deenuji_Loganathan_ Profile Picture
    6,105 Super User 2025 Season 1 on 23 Sep 2024 at 16:42:54
    how to get the total number pages from a pdf file
    Hi 
     
    Please find the below example which provided in the Power automate desktop to get No. of pages in pdf file.
     
     
    Code also attached below for your quicker reference:(Copy and paste into your power automate desktop designer):
     
    # The custom form prompts users to select a PDF file to count its pages.
    @@statistics_TextBlock: '1'
    @@statistics_Input_File: '1'
    @@statistics_Action_Submit: '1'
    Display.ShowCustomDialog CardTemplateJson: '''{
      \"type\": \"AdaptiveCard\",
      \"version\": \"1.4\",
      \"id\": \"AdaptiveCard\",
      \"body\": [
        {
          \"type\": \"TextBlock\",
          \"id\": \"Text block\",
          \"size\": \"medium\",
          \"weight\": \"bolder\",
          \"text\": \"${Text_block_Text}\"
        },
        {
          \"type\": \"Input.File\",
          \"id\": \"File input\",
          \"isRequired\": true,
          \"label\": \"${File_input_Label}\"
        }
      ],
      \"actions\": [
        {
          \"type\": \"Action.Submit\",
          \"id\": \"Ok\",
          \"title\": \"${Ok_Title}\"
        }
      ]
    }''' CustomFormData=> CustomFormData ButtonPressed=> ButtonPressed @Text_block_Text: $'''Find the number of pages in PDF''' @File_input_Label: $'''Select a PDF file:''' @Ok_Title: $'''OK'''
    /# The 'If' action checks whether the user pressed the 'OK' button in the custom form. If yes, the main logic of the flow runs.
    #/
    IF ButtonPressed = $'''Ok''' THEN
        # The following actions retrieve the file path of the selected file. The retrieved directory part of the path is used as a destination folder for the extracted temporary file. The 'Count' variable increases for each page in the PDF file.
        File.GetPathPart File: CustomFormData['File input'] RootPath=> RootPath Directory=> Directory FileName=> FileName FileNameWithoutExtension=> FileNameNoExtension Extension=> FileExtension
        SET Count TO 0
        # The loop repeats until the 'Extract PDF file pages to new PDF file' action fails. When the action fails, the file doesn't have more pages. The 'Extract PDF file pages to new PDF file' action creates a temporary file for each page and the 'Delete file(s)' deletes it.
        LOOP WHILE ($'''True''') = ($'''True''')
            Pdf.ExtractPages PDFFile: CustomFormData['File input'] PageSelection: Count + 1 ExtractedPDFPath: $'''%Directory%\\Temp PDF from a desktop flow''' IfFileExists: Pdf.IfFileExists.AddSequentialSuffix ExtractedPDFFile=> ExtractedPDF
            ON ERROR PageOutOfBoundsError
                GOTO Finish
            END
            File.Delete Files: ExtractedPDF
            Variables.IncreaseVariable Value: Count IncrementValue: 1
        END
    END
    LABEL Finish
    Display.ShowMessageDialog.ShowMessage Title: $'''Flow finished running...''' Message: $'''The selected PDF file has %Count% pages.''' Icon: Display.Icon.None Buttons: Display.Buttons.OK DefaultButton: Display.DefaultButton.Button1 IsTopMost: True ButtonPressed=> ButtonPressed2
    
     
     
    Thanks,
    Deenu

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

Understanding Microsoft Agents - Introductory Session

Confused about how agents work across the Microsoft ecosystem? Register today!

Warren Belz – Community Spotlight

We are honored to recognize Warren Belz as our May 2025 Community…

Congratulations to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Power Automate - Power Automate Desktop

#1
eetuRobo Profile Picture

eetuRobo 18 Super User 2025 Season 1

#2
Nived_Nambiar Profile Picture

Nived_Nambiar 10 Super User 2025 Season 1

#3
stampcoin Profile Picture

stampcoin 6

Overall leaderboard