Replace 'Page Title' of TITLE commands in SPSS with actual title
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | * Alternative to http://www.spsstools.net/Scripts/OutputDoc/ReplaceLeftPanePageTitleByContentOfRightPaneTitle.sbs. * The script should be applied to generated Output with some Page Titles inserted (see Insert...New Page Title menu or TITLE syntax command. After this script run you should see Page Titles in the Outline (left pane of the Output window) are changed to the actual Page Title contents. * This could be used with a user-defined menu button, or with an INSERT at the end of a long (set of) syntaxes. * Much easier to navigate the output this way!. *** This macro call is just to insert some titles for illustration. define !demo () !do !year = 1999 !to 2015 title !quote(!concat("** Year: ", !year)). !doend. !enddefine. !demo. *** And this is the script itself. begin program. |
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | # -*- coding: utf-8 -*- ############################################################################### # Title: Replace 'Page Title' of TITLE commands in SPSS with actual title. # Author: Albert-Jan Roskam # Date: 2014-01-21 # Note: This is entirely aesthetical, but it enhances readability of the output. ############################################################################### import tempfile, os, sys, io import spss, SpssClient def _get_encoding(): """Returns the encoding of the syntax file (utf-8 if unicode mode, ansi if codepage mode (e.g. 1252)""" spsslocale, utf8mode = map(spss.GetSetting, ["locale", "unicode"]) codepage = spsslocale.split(".")[-1] return "utf-8" if utf8mode == "Yes" else codepage def _titleToPane(): """See titleToPane(). This function does the actual job""" outputDoc = SpssClient.GetDesignatedOutputDoc() outputItemList = outputDoc.GetOutputItems() textFormat = SpssClient.DocExportFormat.SpssFormatText filename = tempfile.mktemp() + ".txt" for index in range(outputItemList.Size()): outputItem = outputItemList.GetItemAt(index) if outputItem.GetDescription() == u"Page Title": #This should be changed in case your Output localized. #Say, for Russian localization this would be #if outputItem.GetDescription() == u"Заголовок страницы": outputItem.ExportToDocument(filename, textFormat) with io.open(filename, encoding=_get_encoding()) as f: # alas, must write to an actual file outputItem.SetDescription(f.read().rstrip()) os.remove(filename) return outputDoc def titleToPane(spv=None): """Copy the contents of the TITLE command of the designated output document to the left output viewer pane. This script could be called outside SPSS as well. That is why there is an optional argument of .spv document name to process. If no argument provided the current designated output is processed. """ try: outputDoc = None SpssClient.StartClient() if spv: SpssClient.OpenOutputDoc(spv) outputDoc = _titleToPane() if spv and outputDoc: outputDoc.SaveAs(spv) except: print "ERROR filling TITLE in Output Viewer [%s]" % sys.exc_info()[1] finally: try: SpssClient.Exit() except WindowsError: pass SpssClient.StopClient() if __name__ == "__main__": titleToPane() |
80 | end program.
|
Related pages
...
Navigate from here