Skip to main content

Notifications

Power Automate - Power Automate Desktop
Answered

Error Message for Extract PDF file pages into new PDF

Posted on 2 Dec 2024 05:01:13 by 73
Hey there,
 
I tried to splits PFD files in one folder to each and it didn't finish the execution and then stopped. So I only have partial of PDF file split. (see below)  
 
As below, I tried to extract all PDF stayed in PAD PDF 0 folder (in yellow) and split each of those PDF files to each per page int 'PAD PDF 1' folder naming with variable name without extension (in yellow) , but got below error message. 
 
Would you help to take a look into that issue? Thanks!
 
 
 
There are total 19 pages in 9-26 BOL.PDF, but it only got 4 splits. 
 
Categories:
  • xuyinaoyixue1 Profile Picture
    xuyinaoyixue1 73 on 02 Dec 2024 at 17:04:57
    Error Message for Extract PDF file pages into new PDF
    Thank you so much, VishnuReddy1997 and Deenuji_Loganathan_, you both solved my problem! Thanks!
  • Verified answer
    VishnuReddy1997 Profile Picture
    VishnuReddy1997 2,161 on 02 Dec 2024 at 05:43:37
    Error Message for Extract PDF file pages into new PDF
     
    Please find the below solution.
     
     
    PAD Code:
     
    Folder.GetFiles Folder: $'''C:\\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:\\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:\\Desktop\\Power Automate Desktop\\Practice\\PDF\\PAD PDF 1\\%CurrentItem.NameWithoutExtension%.pdf''' IfFileExists: Pdf.IfFileExists.AddSequentialSuffix ExtractedPDFFile=> ExtractedPDF
        END
    END
    
     
    Python Code:
     
    Please save the code as .py file as use it in 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}")
     
    (Note:- if you got your solution you can mark as solution and gives kudos)
     
    Thanks & Regards
    Vishnu Reddy
  • Suggested answer
    Deenuji_Loganathan_ Profile Picture
    Deenuji_Loganathan_ 5,903 on 02 Dec 2024 at 05:26:10
    Error Message for Extract PDF file pages into new PDF
    Hi,
     
    You may try the below code:
     
    Code:(Copy and paste the below code into your power automate desktop designer screen):
    **REGION Get No of pages in pdf
    Folder.GetFiles Folder: $'''C:\\Boot\\SP File''' FileFilter: $'''*.pdf''' IncludeSubfolders: False FailOnAccessDenied: True SortBy1: Folder.SortBy.NoSort SortDescending1: False SortBy2: Folder.SortBy.NoSort SortDescending2: False SortBy3: Folder.SortBy.NoSort SortDescending3: False Files=> PDFFiles
    
    LOOP FOREACH PDFFile IN PDFFiles
        SET NumberOfPages TO 0
        LOOP WHILE ($'''True''') = ($'''True''')
            Pdf.ExtractPages PDFFile: PDFFile PageSelection: NumberOfPages + 1 ExtractedPDFPath: $'''C:\\Boot\\pdf\\%PDFFile.Name%''' IfFileExists: Pdf.IfFileExists.AddSequentialSuffix ExtractedPDFFile=> ExtractedPDF
            ON ERROR PageOutOfBoundsError
                GOTO Finish
            END
            Variables.IncreaseVariable Value: NumberOfPages IncrementValue: 1
        END
        LABEL Finish
    
    END
    **ENDREGION
    
     
    Screenshot for your reference:
     
    Thanks,
    Deenuji Loganathan đź‘©â€Ťđź’»
    Power Automate Desktop Community Champion đź¤–
    Follow me on LinkedIn đź‘Ą

    -------------------------------------------------------------------------------------------------------------
    If I've helped solve your query, kindly mark my response as the solution âś” and like my suggestion ❤️  Your feedback supports future seekers đźš€

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

November 2024 Newsletter…

November 2024 Community Newsletter…

Community Update Oct 28…

Power Platform Community Update…

Tuesday Tip #7 Community Profile Tips…

Welcome to a brand new series, Tuesday Tips…

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 143,297

#2
RandyHayes Profile Picture

RandyHayes 76,308

#3
Pstork1 Profile Picture

Pstork1 63,890

Leaderboard