Author Topic: Using with python - truncated help  (Read 368 times)

0 Members and 1 Guest are viewing this topic.

ekopta

  • Newbie
  • *
  • Posts: 9
Using with python - truncated help
« on: August 25, 2021, 06:18:20 PM »
I'm taking a stab at writing a python wrapper for PDF-ShellTools, and I'm wondering if anyone else at the forum has any experience with this. Specifically, I'm running PDFShellTools.exe with the subprocess package. This will suffice to let me work out the logic of what I'm trying to build, though I'd love to hear if anyone has used the dll from python. Heck, which dll?

First things first, I've hit a snag trying to capture the help message. When I don't capture the output, the full message is displayed in the Windows command line terminal. However, trying to capture the output to stdout only gets the first three lines (version, copyright, and link). Where did the rest go? How do I get that too?

Here's a MWE:
Code: (python) [Select]
import subprocess


class PdfTool(object):
    """Base class for PDF-ShellTools function classes."""

    def __init__(self, functionname=None, rundll=False):
        """Initialize class."""
        self.functionname = functionname
        self.rundll = rundll
        self.exename = 'PdfShellTools'
        self.params = []
        self.runsettings = {}

    def _run_dll(self):
        """Run tool via dll."""
        pass

    def _run_exe(self, captureoutput=False):
        """Run tool via command line exe."""
        toolargs = [self.exename, ]
        if self.functionname is not None:
            toolargs.append(self.functionname)
        cp = subprocess.run(toolargs, capture_output=captureoutput)
        return cp

    def run(self, captureoutput=False):
        """Run the tool in the preferred way."""
        if self.rundll:
            processdict = self._run_dll()
        else:
            processdict = self._run_exe(captureoutput=captureoutput)
        return processdict


def pdf_shelltools_help(captureoutput=False):
    """Return top-level PDF-ShellTools help."""
    return PdfTool().run(captureoutput=captureoutput)


print('###############################################################')
print('Output not captured')
print(pdf_shelltools_help())
print('\n\n')
print('###############################################################')
print('Output captured\n\n')
print(pdf_shelltools_help(captureoutput=True))



RTT

  • Administrator
  • *****
  • Posts: 886
Re: Using with python - truncated help
« Reply #1 on: August 26, 2021, 08:33:38 PM »
I'm taking a stab at writing a python wrapper for PDF-ShellTools, and I'm wondering if anyone else at the forum has any experience with this. Specifically, I'm running PDFShellTools.exe with the subprocess package. This will suffice to let me work out the logic of what I'm trying to build, though I'd love to hear if anyone has used the dll from python. Heck, which dll?
Can't help you with the python language, but it's the pdfshelltools.dll that exports the tool functions. These exports are the same used by the command line interface tool pdfshelltools.exe.
The exported functions use the same prototype as the functions that can be run by the rundll32.exe Windows tool. It is explained in the user's guide DLL interface topic.
Take note you must use a 32-bit version of Python, to use the DLL interface, as the pdfshelltools.dll is 32-bit.

Quote
First things first, I've hit a snag trying to capture the help message. When I don't capture the output, the full message is displayed in the Windows command line terminal. However, trying to capture the output to stdout only gets the first three lines (version, copyright, and link). Where did the rest go? How do I get that too?
Don't know exactly why this is happening, but I'm going to make some changes to fix this issue in the next release.

The help data is in a xml file, embedded as a resource in the PDFShellTools.exe, that for sure you can extract and process using python.
You can use a tool such as the Resource Hacker to check the PDFShellTools.exe resources.