Skip to content

Instantly share code, notes, and snippets.

@pigreco
Created September 13, 2023 16:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pigreco/272e58381ac896a86acbd4ccc1bc7fce to your computer and use it in GitHub Desktop.
Save pigreco/272e58381ac896a86acbd4ccc1bc7fce to your computer and use it in GitHub Desktop.
Funzione personalizzate per field clac by Giulio
# Korto19 2023
from qgis.core import *
from qgis.gui import *
from PyPDF2 import PdfReader
@qgsfunction(group='HfcQGIS',referenced_columns=[])
def Get_Pdf_N_Pages(pdfpath):
"""
Restituisce il numero di pagine di un pdf dando il percorso
<h2>Example usage:</h2>
<ul>
<li>Get_Pdf_N_Pages(pdfpath)-> 3</li>
</ul>
"""
reader = PdfReader(str(pdfpath))
number_of_pages = len(reader.pages)
return number_of_pages
@pigreco
Copy link
Author

pigreco commented Sep 13, 2023

errore su QGIS 3.28.10 LTR Firenze

Errore Valutazione: Get_Pdf_N_Pages() takes 1 positional argument but 3 were given:
Traceback (most recent call last):
  File "C:\OSGeo4W/apps/qgis-ltr/./python\qgis\core\additions\qgsfunction.py", line 81, in func
    return self.function(*values)
TypeError: Get_Pdf_N_Pages() takes 1 positional argument but 3 were given

Versione Python 3.9.5

@pigreco
Copy link
Author

pigreco commented Sep 13, 2023

Questa funziona

# Korto19 2023
from qgis.core import *
from qgis.gui import *
from PyPDF2 import PdfReader

@qgsfunction(group='HfcQGIS',referenced_columns=[])
def Get_Pdf_N_Pages(pdfpath, *args, **kwargs):
    """
    Restituisce il numero di pagine di un pdf dando il percorso
    <h2>Example usage:</h2>
    <ul>
      <li>Get_Pdf_N_Pages(pdfpath)-> 3</li>
    </ul>
    """

    reader = PdfReader(str(pdfpath))
    number_of_pages = len(reader.pages)

    return number_of_pages

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment