Guest User

Untitled

a guest
Mar 18th, 2015
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.86 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. import os
  4. import spss
  5. import SpssClient
  6.  
  7.  
  8. def getSyntaxPath():
  9.     """Get the file path of the designated syntax, if any"""
  10.     SyntaxDoc = SpssClient.GetDesignatedSyntaxDoc()
  11.     if SyntaxDoc:
  12.         syntaxPath = SyntaxDoc.GetDocumentPath()
  13.         if not syntaxPath:
  14.             syntaxPath = os.getcwd()
  15.     else:
  16.         SyntaxDoc = SpssClient.NewSyntaxDoc()
  17.         syntaxPath = os.getcwd()
  18.     SyntaxDoc.SetAsDesignatedSyntaxDoc()
  19.     return syntaxPath
  20.  
  21. def keepPivotTables(flename):
  22.     MyOutputDocLists = SpssClient.GetOutputDocuments()
  23.     MyOutputDoc = SpssClient.GetDesignatedOutputDoc()
  24.    
  25.     # SAVE BEFORE ANYTHING
  26.     spv_filename = os.path.splitext(flename)[0] + "_ORG_.spv")
  27.     MyOutputDoc.SaveAs(spv_filename)
  28.    
  29.     MyOutputDoc.ClearSelection()
  30.     MyOutputDoc.SelectAllCharts()
  31.     MyOutputDoc.Delete()
  32.     MyOutputDoc.SelectAllLogs()
  33.     MyOutputDoc.Delete()
  34.     MyOutputDoc.SelectAllNotes()
  35.     MyOutputDoc.Delete()
  36.     MyOutputDoc.SelectAllText()
  37.     MyOutputDoc.Delete()
  38.     MyOutputDoc.SelectAllTitles()
  39.     MyOutputDoc.Delete()
  40.     MyOutputDoc.SelectAllWarnings()
  41.     MyOutputDoc.Delete()
  42.  
  43. def setPrintOptions(flename, orientation, margins, start_page_number):
  44.     MyOutputDocLists = SpssClient.GetOutputDocuments()
  45.     MyOutputDoc = SpssClient.GetDesignatedOutputDoc()
  46.     top, left, right, bottom = margins
  47.     printOptions = SpssClient.PrintOptions
  48.     set_print_options = MyOutputDoc.SetPrintOptions
  49.     set_print_options(printOptions.Orientation, orientation)
  50.     set_print_options(printOptions.TopMargin, top)
  51.     set_print_options(printOptions.LeftMargin, left)
  52.     set_print_options(printOptions.RightMargin,right)
  53.     set_print_options(printOptions.BottomMargin, bottom)
  54.     set_print_options(printOptions.StartingPageNumber, start_page_number)
  55.     MyOutputDoc.SaveAs(os.path.splitext(flename)[0] + "_M2_.spv")
  56.  
  57. def write_bookmarks(MyCurrentPath):
  58.     # better to use codecs.open or io.open
  59.     with open(os.path.join(MyCurrentPath, 'Bookmarks1.txt'), 'w') as FileBookM1:
  60.         NewTitle = "Table: " + str(PTable) + " " + CurrentTitle
  61.         MyPivotTable.SelectTitle()
  62.         MyPivotTable.SetTitleText(NewTitle)
  63.         MyPivotTable.ClearSelection()
  64.         FileBookM1.write(">" + NewTitle + "\n")
  65.         if PTable == 1:
  66.             print "YES-TableNumbers"
  67.  
  68. def write_toc(MyCurrentPath):
  69.     with open(os.path.join(MyCurrentPath, 'TOC.txt', 'w') as FileTOC2:
  70.         if UseTableNumbering:
  71.             NewTitleTOC = NewTitle
  72.         else:
  73.             NewTitleTOC = CurrentTitle
  74.         FileTOC2.write(NewTitleTOC + "\n")
  75.         if PTable == 1:
  76.             print "YES-TOC"
  77.  
  78. def write_html_marks(MyCurrentPath):
  79.     with open(os.path.join(MyCurrentPath, 'HTMLMarks.txt', 'w') as FileHTMLMarks:
  80.         if UseTableNumbering:
  81.             NewTitle = r"""<a name=""" + NewTitle + r"""></a>"""
  82.             NewTitleHTMLRef = r"""<a href=#""" + NewTitle + r""">""" + "</a>"
  83.         else:
  84.             NewTitle = r"""<a name=""" + CurrentTitle + r"""></a>"""
  85.             NewTitleHTMLRef = r"""<a href=#""" + CurrentTitle + r""">""" + "</a>"
  86.  
  87.         FileHTMLMarks.write(NewTitle + "\n")
  88.         FileHTMLMarks.write(NewTitleHTMLRef + "\n")
  89.         if PTable == 1:
  90.             print "YES-HTML"
  91.  
  92. def clean():
  93.     MySyntaxDocsList = SpssClient.GetSyntaxDocuments()
  94.     for index in xrange(MySyntaxDocsList.Size(), 0, -1):
  95.         MySyntaxDocs = MySyntaxDocsList.GetItemAt(index - 1)
  96.         MySyntaxDocs.CloseDocument()
  97.  
  98. def save_output(flename):
  99.     """SAVE DESIGNATED OUTPUT DOCUMENT"""
  100.     MyOutputDoc.SetPromptToSave(True)
  101.     MyOutputDoc.SaveAs(os.path.splitext(flename)[0] + "_FNL_.spv")
  102.  
  103. def export(outputdoc, filename):
  104.     extension = os.path.splitext(filename)[1].lower()
  105.     fmt = SpssClient.DocExportFormat
  106.     formats = {".pdf": fmt.SpssFormatPdf,
  107.                ".xls": fmt.SpssFormatXls,
  108.                ".htm": fmt.SpssFormatHtml,
  109.                ".ppt": fmt.SpssFormatPpt}
  110.     subset = SpssClient.SpssExportSubset.SpssVisible
  111.     try:
  112.         outputdoc.ExportDocument(subset, filename, formats[extension])
  113.     except KeyError:
  114.         raise ValueError("Unknown file extension '%s'" % extension)
  115.  
  116. def walk_through_output(flename, seTableOfContents, useHTMLMarks,
  117.                         cleanUp, output_formats):
  118.     MyOutputDoc = SpssClient.GetDesignatedOutputDoc()
  119.     MyOutputItems = MyOutputDoc.GetOutputItems()
  120.     fn = os.path.splitext(flename)[0]
  121.    
  122.     PTable = 0
  123.     for index in xrange(MyOutputItems.Size()):
  124.         MyOutputItem = MyOutputItems.GetItemAt(index)
  125.         if MyOutputItem.GetType() == SpssClient.OutputItemType.PIVOT:
  126.    
  127.             PTable += 1
  128.    
  129.             MyPivotTable = MyOutputItem.GetSpecificType()
  130.             MyPivotTable.SetUpdateScreen(True)
  131.             MyPivotTable.ClearSelection()
  132.             CurrentTitle = MyPivotTable.GetTitleText()
  133.    
  134.             if PTable > 1:
  135.                 MyOutputItem.SetPageBreak(True)
  136.    
  137.             if UseTableNumbering:
  138.                 write_bookmarks(MyCurrentPath)
  139.    
  140.             if UseTableOfContents:
  141.                 write_toc(MyCurrentPath)
  142.      
  143.             if UseHTMLMarks:
  144.                 write_html_marks(MyCurrentPath)
  145.    
  146.             # SAVE POST NUMBERING, ETC
  147.             if UseTableNumbering or UseTableOfContents or UseHTMLMarks:
  148.                 MyOutputDoc.SaveAs(fn + "_M3_.spv")
  149.      
  150.             if CleanUp:
  151.                 clean()
  152.    
  153.             for output_format in output_formats:
  154.                 output_format = output_format.lower()
  155.                 if output_format == ".spv":
  156.                     save_output(flename)
  157.                 else:
  158.                     export(outputdoc, fn + output_format)
  159.    
  160.     else:
  161.         print "No output found"
  162.  
  163. def main(orientation, margins, start_page_number, useTableNumbering,
  164.              useTableOfContents, useHTMLMarks, cleanUp, output_formats):
  165.     flename = getSyntaxPath()
  166.     MyCurrentPath = os.path.dirname(flename)
  167.     keepPivotTables(flename)
  168.     setPrintOptions(flename, orientation, margins, start_page_number)
  169.     walk_through_output(flename, seTableOfContents, useHTMLMarks,
  170.                         cleanUp, output_formats)
  171.  
  172.  
  173. def outputRoutine(orientation="2",
  174.                   margins=("18", "9", "9", "18"),
  175.                   start_page_number="1"
  176.                   useTableNumbering = True,
  177.                   useTableOfContents = False,
  178.                   useHTMLMarks = False,
  179.                   cleanUp = True,
  180.                   output_formats = ('.spv', '.pdf', '.htm'):
  181.     """Wrapper for main() function"""
  182.     try:
  183.         SpssClient.StartClient()
  184.         main(orientation, margins, start_page_number, useTableNumbering,
  185.              useTableOfContents, useHTMLMarks, cleanUp, output_formats)
  186.     finally:
  187.         SpssClient.StopClient()
  188.  
  189.  
  190. if __name__ == "__main__":
  191.     outputRoutine()
Advertisement
Add Comment
Please, Sign In to add comment