ghostscripts in python with watchdog

legaulph at gmail.com legaulph at gmail.com
Wed Feb 12 06:46:18 EST 2020


I'm trying to use ghostscripts with python watchdog.

I want to duplicate the last page of a pdf to another directory using the
same name as the source pdf + page number.

So watchdog will monitor the directory for the pdf and ghostscript will copy
the last page to another directory.

I have this, and not able to figure out how to change the output name and
location.

 

import sys

import os

import time

import logging

from watchdog.observers import Observer

from watchdog.events import LoggingEventHandler

from watchdog.events import PatternMatchingEventHandler

 

if __name__ == "__main__":

    patterns = "*"

    ignore_patterns = ""

    ignore_directories = False

    case_sensitive = True

    my_event_handler = PatternMatchingEventHandler(patterns,
ignore_patterns, ignore_directories, case_sensitive)

    def on_created(event):

        number_of_pages = 4

        input_pdf = event.src_path

        for i in range(4, number_of_pages +1):

            os.system("gswin64c -q -dBATCH -dNOPAUSE
-sOutputFile=page{page:04d}.pdf"

                    " -dFirstPage={page} -dLastPage={page}"

                    " -sDEVICE=pdfwrite {input_pdf}"

                    .format(page=i, input_pdf=input_pdf))

 

    my_event_handler.on_created = on_created

 

    path = "."

    go_recursively = True

    my_observer = Observer()

    my_observer.schedule(my_event_handler, path, recursive=go_recursively)

 

    my_observer.start()

    try:

        while True:

            time.sleep(1)

    except KeyboardInterrupt:

        my_observer.stop()

        my_observer.join()

 



More information about the Python-list mailing list