Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- import os
- import spss
- import SpssClient
- def getSyntaxPath():
- """Get the file path of the designated syntax, if any"""
- SyntaxDoc = SpssClient.GetDesignatedSyntaxDoc()
- if SyntaxDoc:
- syntaxPath = SyntaxDoc.GetDocumentPath()
- if not syntaxPath:
- syntaxPath = os.getcwd()
- else:
- SyntaxDoc = SpssClient.NewSyntaxDoc()
- syntaxPath = os.getcwd()
- SyntaxDoc.SetAsDesignatedSyntaxDoc()
- return syntaxPath
- def keepPivotTables(flename):
- MyOutputDocLists = SpssClient.GetOutputDocuments()
- MyOutputDoc = SpssClient.GetDesignatedOutputDoc()
- # SAVE BEFORE ANYTHING
- spv_filename = os.path.splitext(flename)[0] + "_ORG_.spv")
- MyOutputDoc.SaveAs(spv_filename)
- MyOutputDoc.ClearSelection()
- MyOutputDoc.SelectAllCharts()
- MyOutputDoc.Delete()
- MyOutputDoc.SelectAllLogs()
- MyOutputDoc.Delete()
- MyOutputDoc.SelectAllNotes()
- MyOutputDoc.Delete()
- MyOutputDoc.SelectAllText()
- MyOutputDoc.Delete()
- MyOutputDoc.SelectAllTitles()
- MyOutputDoc.Delete()
- MyOutputDoc.SelectAllWarnings()
- MyOutputDoc.Delete()
- def setPrintOptions(flename, orientation, margins, start_page_number):
- MyOutputDocLists = SpssClient.GetOutputDocuments()
- MyOutputDoc = SpssClient.GetDesignatedOutputDoc()
- top, left, right, bottom = margins
- printOptions = SpssClient.PrintOptions
- set_print_options = MyOutputDoc.SetPrintOptions
- set_print_options(printOptions.Orientation, orientation)
- set_print_options(printOptions.TopMargin, top)
- set_print_options(printOptions.LeftMargin, left)
- set_print_options(printOptions.RightMargin,right)
- set_print_options(printOptions.BottomMargin, bottom)
- set_print_options(printOptions.StartingPageNumber, start_page_number)
- MyOutputDoc.SaveAs(os.path.splitext(flename)[0] + "_M2_.spv")
- def write_bookmarks(MyCurrentPath):
- # better to use codecs.open or io.open
- with open(os.path.join(MyCurrentPath, 'Bookmarks1.txt'), 'w') as FileBookM1:
- NewTitle = "Table: " + str(PTable) + " " + CurrentTitle
- MyPivotTable.SelectTitle()
- MyPivotTable.SetTitleText(NewTitle)
- MyPivotTable.ClearSelection()
- FileBookM1.write(">" + NewTitle + "\n")
- if PTable == 1:
- print "YES-TableNumbers"
- def write_toc(MyCurrentPath):
- with open(os.path.join(MyCurrentPath, 'TOC.txt', 'w') as FileTOC2:
- if UseTableNumbering:
- NewTitleTOC = NewTitle
- else:
- NewTitleTOC = CurrentTitle
- FileTOC2.write(NewTitleTOC + "\n")
- if PTable == 1:
- print "YES-TOC"
- def write_html_marks(MyCurrentPath):
- with open(os.path.join(MyCurrentPath, 'HTMLMarks.txt', 'w') as FileHTMLMarks:
- if UseTableNumbering:
- NewTitle = r"""<a name=""" + NewTitle + r"""></a>"""
- NewTitleHTMLRef = r"""<a href=#""" + NewTitle + r""">""" + "</a>"
- else:
- NewTitle = r"""<a name=""" + CurrentTitle + r"""></a>"""
- NewTitleHTMLRef = r"""<a href=#""" + CurrentTitle + r""">""" + "</a>"
- FileHTMLMarks.write(NewTitle + "\n")
- FileHTMLMarks.write(NewTitleHTMLRef + "\n")
- if PTable == 1:
- print "YES-HTML"
- def clean():
- MySyntaxDocsList = SpssClient.GetSyntaxDocuments()
- for index in xrange(MySyntaxDocsList.Size(), 0, -1):
- MySyntaxDocs = MySyntaxDocsList.GetItemAt(index - 1)
- MySyntaxDocs.CloseDocument()
- def save_output(flename):
- """SAVE DESIGNATED OUTPUT DOCUMENT"""
- MyOutputDoc.SetPromptToSave(True)
- MyOutputDoc.SaveAs(os.path.splitext(flename)[0] + "_FNL_.spv")
- def export(outputdoc, filename):
- extension = os.path.splitext(filename)[1].lower()
- fmt = SpssClient.DocExportFormat
- formats = {".pdf": fmt.SpssFormatPdf,
- ".xls": fmt.SpssFormatXls,
- ".htm": fmt.SpssFormatHtml,
- ".ppt": fmt.SpssFormatPpt}
- subset = SpssClient.SpssExportSubset.SpssVisible
- try:
- outputdoc.ExportDocument(subset, filename, formats[extension])
- except KeyError:
- raise ValueError("Unknown file extension '%s'" % extension)
- def walk_through_output(flename, seTableOfContents, useHTMLMarks,
- cleanUp, output_formats):
- MyOutputDoc = SpssClient.GetDesignatedOutputDoc()
- MyOutputItems = MyOutputDoc.GetOutputItems()
- fn = os.path.splitext(flename)[0]
- PTable = 0
- for index in xrange(MyOutputItems.Size()):
- MyOutputItem = MyOutputItems.GetItemAt(index)
- if MyOutputItem.GetType() == SpssClient.OutputItemType.PIVOT:
- PTable += 1
- MyPivotTable = MyOutputItem.GetSpecificType()
- MyPivotTable.SetUpdateScreen(True)
- MyPivotTable.ClearSelection()
- CurrentTitle = MyPivotTable.GetTitleText()
- if PTable > 1:
- MyOutputItem.SetPageBreak(True)
- if UseTableNumbering:
- write_bookmarks(MyCurrentPath)
- if UseTableOfContents:
- write_toc(MyCurrentPath)
- if UseHTMLMarks:
- write_html_marks(MyCurrentPath)
- # SAVE POST NUMBERING, ETC
- if UseTableNumbering or UseTableOfContents or UseHTMLMarks:
- MyOutputDoc.SaveAs(fn + "_M3_.spv")
- if CleanUp:
- clean()
- for output_format in output_formats:
- output_format = output_format.lower()
- if output_format == ".spv":
- save_output(flename)
- else:
- export(outputdoc, fn + output_format)
- else:
- print "No output found"
- def main(orientation, margins, start_page_number, useTableNumbering,
- useTableOfContents, useHTMLMarks, cleanUp, output_formats):
- flename = getSyntaxPath()
- MyCurrentPath = os.path.dirname(flename)
- keepPivotTables(flename)
- setPrintOptions(flename, orientation, margins, start_page_number)
- walk_through_output(flename, seTableOfContents, useHTMLMarks,
- cleanUp, output_formats)
- def outputRoutine(orientation="2",
- margins=("18", "9", "9", "18"),
- start_page_number="1"
- useTableNumbering = True,
- useTableOfContents = False,
- useHTMLMarks = False,
- cleanUp = True,
- output_formats = ('.spv', '.pdf', '.htm'):
- """Wrapper for main() function"""
- try:
- SpssClient.StartClient()
- main(orientation, margins, start_page_number, useTableNumbering,
- useTableOfContents, useHTMLMarks, cleanUp, output_formats)
- finally:
- SpssClient.StopClient()
- if __name__ == "__main__":
- outputRoutine()
Advertisement
Add Comment
Please, Sign In to add comment