Ask Your Question
0

Automated sld to pdf printing

asked 2020-05-05 07:46:51 -0500

haugstenrod gravatar image

(PSSE v 33.12) I have a bunch of case files for which I want to automate printing of .slds to pdfs.

I do not want to use the "export to image"-functionality as the graphics are pixel based and does not provide good enough resolution.

I tried to do this by simply printing to pdf as I would have done manually by using printdiagfile(), but by looking at the API, it seems there is no way to set the pdf document name other than stopping and doing this manually for each slider I want to export.

Is it possible to set the pdf-name when using printdiagfile() in the python script, so that I don't have to manually enter this, or do I have to have a look at other options in order to figure out how to automate this process?

edit retag flag offensive close merge delete

3 answers

Sort by ยป oldest newest most voted
0

answered 2022-06-17 05:33:43 -0500

klbe gravatar image

Great suggestions here! By running the script from a opened case file in PSSE, the default location for the printer is the current working folder for the case file.

I used:

psspy.printdiagfile(r"""Microsoft Print to PDF""", copies, orientation)

The name of the pdf-file is found by adding ".pdf" to the name of the sld-file. I used the following to rename it:

os.rename(oldPDFname, newPDFname)

This was performed in v35.

edit flag offensive delete link more
0

answered 2020-10-29 08:00:38 -0500

the--duke gravatar image

You can do it indirectly by changing your printer

step 1 Download a pdf printing software that allows hiding the dialog windows

step 2 (this has to be done only once) Find the printer settings and set a default name and location for your pdfs, either (a) as default or (b) using macros that can be handled by your python script (i prefer to have a default name and change it later within the script) Also suppress all dialog and error boxes

step 3 Use psspy.printdiagfile('your-printer',#copies,orientation) This should print your sld with the default printer settings

step 4 locate the pdf using python and change the name and location to whatever you want [option (a) of step 2] OR modify your python code such that it is compatible with the printer's macros [option (b) of step 2]

step 5 repeat until the last case

it should work

edit flag offensive delete link more
0

answered 2020-05-06 21:49:07 -0500

oppossumX gravatar image

updated 2020-05-08 00:35:07 -0500

I've used printdiagfile() to batch print pdfs. I think it is sensitive to the printer you are using to print to pdf (eg. Adobe, Microsoft, etc). Some pdf software has settings for location, automatic file naming, and options to append to existing files. I think Adobe defaults the filename to the same as the SLD filename and you need to print one SLD manually to set where to save the files.

I used "win32gui" python module to find the "Save PDF File As" window and use "SendKeys" python module to send the inputs automatically with python.

edit: It's 2 separate scripts, 1 script ran from the PSSE GUI to handle all of the opening, modification, saving, and printing of the PSSE SLDs. The other script run concurrently (not in psse tho) to detect and interact with the pop-up windows. An alternate to the second script is to just hold the enter key down. This is sensitive to the printer.

import os
import psspy

def exportAsPDF():
    os.system("TASKKILL /F /IM acrobat.exe")
    psspy.refreshdiagfile()
    psspy.printdiagfile(r"""Adobe PDF""",1,1) # (Name of printer, # of copies, 1=Portrait/2=landscape)
    os.system("TASKKILL /F /IM acrobat.exe")

import win32gui
import re
import SendKeys

class WindowFinder:
        # """Class to find and make focus on a particular Native OS dialog/Window """
    def __init__ (self):
        self._handle = None

    def find_window(self, class_name, window_name = None):
        # """Pass a window class name & window name directly if known to get the window """
        self._handle = win32gui.FindWindow(class_name, window_name)

    def _window_enum_callback(self, hwnd, wildcard):
        # '''Call back func which checks each open window and matches the name of window using reg ex'''
        if re.match(wildcard, str(win32gui.GetWindowText(hwnd))) != None:
            self._handle = hwnd

    def find_window_wildcard(self, wildcard):
        # """ This function takes a string as input and calls EnumWindows to enumerate through all open windows """
        self._handle = None
        win32gui.EnumWindows(self._window_enum_callback, wildcard)

    def set_foreground(self):
        # """Get the focus on the desired open window"""
        win32gui.SetForegroundWindow(self._handle)

win = WindowFinder()
while True:
    try:
        win.find_window_wildcard(".*Save PDF File As.*") 
        win.set_foreground()
        SendKeys.SendKeys("{ENTER}")           #Use SendKeys to send ENTER key stroke to Save As dialog

        win.find_window_wildcard(".*Confirm Save As.*") 
        win.set_foreground()
        SendKeys.SendKeys("{Y}")           #Use SendKeys to send Y key stroke to Save As dialog
    except:
        pass
edit flag offensive delete link more

Comments

Very interesting. Would you post a demo script about it?

jconto gravatar imagejconto ( 2020-05-07 13:53:21 -0500 )edit

Thank you, this seems to work like a charm! Only minor thing is that my background script (script 2) didn't want to exit the while loop after saving the pdf's, but continued searching for new windows, so I added a time based exit criteria using "time.time()" Anyway - thumbs up!

haugstenrod gravatar imagehaugstenrod ( 2020-05-08 02:23:18 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

[hide preview]

Question Tools

2 followers

Stats

Asked: 2020-05-05 07:46:51 -0500

Seen: 3,175 times

Last updated: Jun 17 '22