PDF-ShellTools > General

Using dll with python

(1/1)

ekopta:
I've found the way to use PdfShellTools.dll with python, and here's the basic recipe. This doesn't format the text input to all the particular specifications PdhShellTools requires; it's just about getting it to the dll.


--- Code: ---from ctypes import c_int, WINFUNCTYPE, WinDLL
from ctypes.wintypes import HWND, HINSTANCE, LPSTR


def setup():
    """Set dll up to run."""
    DLLNAME = 'C:/Program Files (x86)/PDF-ShellTools/PDFShellTools.dll'
    dll = WinDLL(DLLNAME)
    prototype = WINFUNCTYPE(None, HWND, HINSTANCE, LPSTR, c_int)
    paramflags = ((1, 'hwnd', 0), (1, 'hinst', 0), (1, 'IpszCmdLine', ""),
                  (1, 'nCmdShow', 0))
    return (dll, prototype, paramflags)


def make_function(pdfstname, dll, prototype, paramflags):
    """Make a function to run PdfShellTools action from python."""
    return prototype((pdfstname, dll), paramflags)


def run_dll_function(function, param, file):
    """Run function in dll. Format param & file nicely elsewhere."""
    dllinput = param + " " + file
    function(IpszCmdLine=LPSTR(dllinput.encode('utf-8')))


dll, prototype, paramflags = setup()
set_metadata = make_function('SetMetadata', dll, prototype, paramflags)
pdfstparams = '"Metadata=Title=SomeTitle"'
pdfstfiles = 'afile.pdf'
run_dll_function(set_metadata, pdfstparams, pdfstfiles)

--- End code ---

My guess is that you only want to do the setup part once to realize the full speed gain. It's probably good to minimize the calls to make_function as well. I haven't looked into performance concerns yet.

ekopta:
I should have mentioned that the dll only works with 32-bit Python installations.  :-[

Navigation

[0] Message Index

Go to full version