from pathlib import Path
import re
from re import search as __s

def count( vPath ):
    """
    Takes one argument: the path where you want to search the files.
    Returns a dictionary with the file name and number of pages for each file.
    """
    with open(vPath, errors="ignore") as f:
        # Hardcoded exceptions
        if str(vPath) == "Glory and Praise.pdf":
            return 1
        elif str(vPath) == "Pie Jesu from Requiem Andrew Lloyd.pdf":
            return 5
        elif str(vPath) == "The Prayer.pdf":
            return 8
        elif str(vPath) == "Song for the Nations.pdf":
            return 2
        # print(vPath)
        return int(re.search(r'/Count (\d*)',f.read()).group(1))
        # for line in f:
            # try:
                # if '/Count' in line:
                    
            # except TypeError:
                # pass

l = ''
s = ''
t = 1
paths = list(Path('.').glob('*.pdf'))
paths.sort()
for p in paths:
    if str(p) not in ['All.pdf','Words.pdf','Laudate.pdf','Mass of Christ the savior Dan Schutte.pdf','The best of bukas palad volume2.pdf'] and "202" not in str(p):
        # print(p)
        l += '"{}" '.format(p)
        # print(count(p))
        s += f"[/Title ({p.stem}) /Page {t} /OUT pdfmark\n"
        t += count(p)
        
print("mgs -o All.pdf -sDEVICE=pdfwrite "+l+"all_pdfmarks.gs")
# print(s)
with open('all_pdfmarks.gs','w') as f:
    f.write(s)
    
# (examples/tiger.eps) run [ /Page 1 /Title (tiger.eps) /OUT pdfmark
# (examples/colorcir.ps) run [ /Page 2 /Title (colorcir.ps) /OUT pdfmark
# mgs -o All.pdf -sDEVICE=pdfwrite "Alive Forevermore.pdf" "All the Ends of the Earth.pdf" "Amazing Grace.pdf" "Anima Christi.pdf" "As the Deer.pdf" "Ave Maria.pdf" "Beyond the Days.pdf" "Blessed be God.pdf" "Blest are They.pdf" "Canticles of the Sun.pdf" "Children of Light and of the Morning.pdf" "City of God.pdf" "Come Holy Spirit.pdf" "Come to the Savior.pdf" "Create in me a clean heart.pdf" "Give Thanks.pdf" "Glory and Praise.pdf" "God is so Good.pdf" "Here at This Table.pdf" "Here I am, Lord.pdf" "Here in This Place.pdf" "Hosea.pdf" "How Great Thou Art.pdf" "I offer my life.pdf" "I Surrender All.pdf" "I Will Sing Forever.pdf" "In bread we bring you lord.pdf" "In Him Alone.pdf" "In His Time.pdf" "Jesus, Keep Me Near the Cross.pdf" "Jesus, Lamb of God.pdf" "Joyful, Joyful, We Adore Thee.pdf" "Lead Me Lord.pdf" "Lead Me, Lord.pdf" "Let Heaven Rejoice.pdf" "Let There Be Love.pdf" "Let There Be Peace on Earth.pdf" "Lift up your hands.pdf" "Lord I Give You My Heart.pdf" "Lord, I Lift Your Name on High.pdf" "Lord, We Touch You Today.pdf" "Mighty To Save.pdf" "Morning has broken.pdf" "MySuperPDF.pdf" "Old Rugged Cross.pdf" "On This Day, O Beautiful Mother.pdf" "One Bread, One Body.pdf" "Open the Eyes of My Heart.pdf" "Power of your love.pdf" "Prayer for Generosity.pdf" "Prayer of Rupert Mayer.pdf" "Seek ye first.pdf" "Shine, Jesus, Shine.pdf" "Shout to the Lord.pdf" "Sing a New Song.pdf" "Sing Alleluia.pdf" "Table of Plenty.pdf" "Take and Receive.pdf" "The Servant King.pdf" "Who am I.pdf" "With One Voice.pdf" "Worthy is the Lamb.pdf" "You are my Hiding Place.pdf" all_pdfmarks.gs
# pdftk A="Words.pdf" B="Let There Be Love.pdf" C="Sing Alleluia.pdf" D="In bread we bring you lord.pdf" E="Power of your love.pdf" F="Give Thanks.pdf" cat A1 B C D E F output 2021-10-31.pdf