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}")
# 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
eetuRobo
18
Super User 2025 Season 1
Nived_Nambiar
10
Super User 2025 Season 1
stampcoin
6