def batchConvert(self, batch=None, outputType="pdf", outputFilePath=False): # try: batch = json.loads(batch) # except: # pass # # try: # process tokens for each document individually files = [] i = 0 for doc, params in batch: mm = pyMailMerge(self.stubsDir + doc) outputFile = mm.convertFile(params, "odt") files.append(outputFile) # some stuff for debugging. # x = open( "/var/www/mms_docs/output/%s_%s.xml" % ( doc, i ), 'w' ) # x.write( params ) # x.close() # i+=1 # open first file mm = pyMailMerge(files[0]) # append every other file to the first for x in files[1:]: mm.joinDocumentToEnd(x) if outputFilePath is not False: # convert file and get path outputFile = mm.convertFile(None, outputType) # copy file to putput dir outputFilePath = os.path.abspath(os.path.join(self.outputDir, outputFilePath)) shutil.copy(outputFile, outputFilePath) # return the filename content = outputFilePath try: os.chmod(outputFilePath, 0777) except: pass # cleanout os.unlink(outputFile) else: content = mm.convert(None, outputType) for x in files: os.unlink(x) return content
def test_deleteColumn(self): path = os.path.abspath( os.path.join( os.path.dirname( __file__ ), 'docs/deleteColumn.odt' ) ) outFile = os.path.join( os.path.dirname( __file__ ), 'docs/deleteColumn.out.odt' ) pmm = pyMailMerge( path ) xml = """<tokens> <token> <name>deletecolumn|wanted</name> <value>0</value> </token> <token> <name>deletecolumn|unwanted</name> <value>1</value> </token> <token> <name>deletecolumn|another</name> <value>0</value> </token> </tokens>""" x = pyMailMerge._readParamsFromXML( xml ) pmm._process( x ) pmm.document.refresh() pmm.document.saveAs( outFile ) results = self._getFirstTableData( outFile ) expected = [[ 'First', "Third" ], [ 'A', 'C' ], [ 'D', 'F' ] ] self.assertEqual( expected, results )
def test_deleteRow(self): path = os.path.abspath( os.path.join( os.path.dirname( __file__ ), 'docs/deleteRow.odt' ) ) outFile = os.path.join( os.path.dirname( __file__ ), 'docs/deleteRow.out.odt' ) pmm = pyMailMerge( path ) xml = """<tokens> <token> <name>deleterow|wanted</name> <value>0</value> </token> <token> <name>deleterow|unwanted</name> <value>1</value> </token> <token> <name>deleterow|another</name> <value>0</value> </token> </tokens>""" x = pyMailMerge._readParamsFromXML( xml ) pmm._process( x ) pmm.document.refresh() pmm.document.saveAs( outFile ) results = self._getFirstTableData( outFile ) expected = [[ 'Wanted Row', '', "Send value '0' to keep" ], [ 'Another Wanted', '', "Send value '0' to keep"] ] self.assertEqual( expected, results )
def test_repeatrow_and_repeatcolumn(self): path = os.path.abspath( os.path.join( os.path.dirname( __file__ ), 'docs/repeat_row_and_column.odt' ) ) outFile = os.path.join( os.path.dirname( __file__ ), 'docs/repeat_row_and_column.out.odt' ) pmm = pyMailMerge( path ) f = open( os.path.abspath( os.path.join( os.path.dirname( __file__ ), 'fixtures/repeatrow_and_repeatcolumn.xml' ) ) ) x = pyMailMerge._readParamsFromXML( f.read() ) pmm._process( x ) pmm.document.refresh() pmm.document.saveAs( outFile ) tableData = self._getFirstTableData( outFile ) expectedOutcome = [ [ 'Name', 'name', 'life_benefit', 'life', 'add', 'dep_life', 'crit_illness', 'eap', 'wi_benefit', 'wi', 'ltd_benefit', 'ltd', 'ehb', 'dental', 'total', "Total" ], [u'Test User', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' '], [u'Test User 2', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' '], [u'', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' '], [u'', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' '], [u'', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' '], [u'', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' '], [u'', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' '], [u'', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' '], [u'', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' '], [u'Another User', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' ', u' '] ] self.assertEquals( expectedOutcome, tableData )
def test_repeatrow_test_for_ted(self): path = os.path.abspath( os.path.join( os.path.dirname( __file__ ), 'docs/repeatrow_test_for_ted.odt' ) ) outFile = os.path.join( os.path.dirname( __file__ ), 'docs/repeatrow_test_for_ted.out.odt' ) pmm = pyMailMerge( path ) f = open( os.path.abspath( os.path.join( os.path.dirname( __file__ ), 'fixtures/repeatrow_test_for_ted.xml' ) ) ) x = pyMailMerge._readParamsFromXML( f.read() ) pmm._process( x ) pmm.document.refresh() pmm.document.saveAs( outFile ) tableData = self._getFirstTableData( outFile ) expectedOutcome = [ [ 'Benefit', 'Assurance', 'Financial'], [ 'Life Benefit', ' - ', ' - '], [ 'Officers', '10000', '10000' ], [ 'Owners/Officers/Managers', '10000', '10000' ], [ 'Dental Benefit', ' - ', ' - ' ], [ 'Employees', '10000', '10000' ], [ 'Officers', '10000', '10000' ], [ 'Health Benefit', ' - ', ' - ' ], [ 'Officers', '10000', '10000' ], [ 'LTD Benefit', ' - ', ' - ' ], [ 'Officers', '10000', '10000' ], [ 'Weekly Income Benefit', ' - ', ' - ' ], [ 'Officers', '10000', '10000' ] ] self.assertEquals( expectedOutcome, tableData ) self.assertEquals( 13, len( tableData ) )
def convert(self, params="", odt="", type="pdf", resave=False, saveExport=False): try: fileName = os.path.abspath(self.outputDir + odt) mms = pyMailMerge(fileName) return mms.convert(params, type, resave, saveExport) except: number = "?" message = "unknown exception" return self.__errorXML(number, message)
def testRepeatSectionTable(self): path = os.path.abspath( os.path.join( os.path.dirname( __file__ ), 'docs/repeatSectionTable.odt' ) ) pmm = pyMailMerge( path ) x = [ { 'token':'repeatsection|first', 'value':'4' } ] pmm._process( x ) pmm.document.refresh() pmm.document.saveAs( os.path.join( os.path.dirname( __file__ ), 'docs/repeatSectionTable.out.pdf' ) )
def calculator(self, params="", ods="", format="json", outputFile=None): # try: fileName = os.path.abspath(self.stubsDir + ods) mms = pyMailMerge(fileName, "ods") if outputFile is not None: data = mms.calculator(params, self.outputDir + outputFile) else: data = mms.calculator(params) # todo xml output print data data = json.dumps(data) return data
def getNamedRanges(self, ods="", format="json"): path = os.path.abspath(self.stubsDir + odt) mms = pyMailMerge(path, "ods") names = mms.getNamedRanges() if format == "xml": xml = """<?xml version="1.0" encoding="UTF-8"?><namedranges>""" for x in names: xml += "<namedrange>%s</namedrange>" % x xml += "</namedranges>" return xml else: return json.dumps(names)
def getTokens(self, odt="", format="json"): try: path = os.path.abspath(self.stubsDir + odt) mms = pyMailMerge(path) tokens = mms.getTokens() if format == "xml": xml = """<?xml version="1.0" encoding="UTF-8"?><tokens>""" for x in tokens: xml += "<token>%s</token>" % x xml += "</tokens>" return xml else: return json.dumps(tokens) except: return self.__errorXML("?", "could not get tokens")
def uploadConvert(self, params="", odt="", type="pdf"): # write temporary file file, fileName = tempfile.mkstemp(suffix="_pyMMS.odt") os.close(file) file = open(fileName, "w") file.write(odt) file.close() # do conversion try: mms = pyMailMerge(fileName) content = mms.convert(params, type) os.unlink(fileName) return content except: number = "?" message = "unknown exception" return self.__errorXML(number, message)
def test_updateChart(self): path = os.path.abspath( os.path.join( os.path.dirname( __file__ ), 'docs/charttest.odt' ) ) outFile = os.path.join( os.path.dirname( __file__ ), 'docs/charttest.out.pdf' ) pmm = pyMailMerge( path ) xml = """<tokens> <token> <name>repeatrow|timeperiod</name> <value>Year - 2009</value> <value>Year - 2010</value> <value>Year - 2011</value> <value>Year - 2012</value> </token> <token> <name>premium</name> <value>$505</value> <value>$1500</value> <value>$1000</value> <value>$1000</value> </token> <token> <name>paidclaims</name> <value>$200</value> <value>$2000</value> <value>$50</value> <value>$3000</value> </token> <token> <name>lossratio</name> <value>34%</value> <value>33%</value> <value>35%</value> <value>35%</value> </token> </tokens>""" x = pyMailMerge._readParamsFromXML( xml ) pmm._process( x ) pmm.document.refresh() pmm.document.saveAs( outFile ) self.assertTrue( True )
def test_getTokens(self): path = os.path.abspath( os.path.join( os.path.dirname( __file__ ), 'docs/invoice.odt' ) ) pmm = pyMailMerge( path ) x = pmm.getTokens() x.sort() self.assertEquals( 0, x.index( '~client::city~' ) ) self.assertEquals( 1, x.index( '~client::company~' ) ) self.assertEquals( 2, x.index( '~client::name~' ) ) self.assertEquals( 3, x.index( '~client::postalcode~' ) ) self.assertEquals( 4, x.index( '~client::prov~' ) ) self.assertTrue( '~company::city~' in x ) self.assertTrue( '~company::phone~' in x ) self.assertTrue( '~company::prov~' in x ) self.assertTrue( '~endif|notpaid~' in x ) self.assertTrue( '~endif|paid~' in x ) self.assertTrue( '~html|notes~' in x ) self.assertTrue( '~if|notpaid~' in x ) self.assertTrue( '~if|paid~' in x ) self.assertTrue( '~paid::date~' in x ) self.assertTrue( '~payment::due~' in x ) self.assertTrue( '~product::qty~' in x ) self.assertTrue( '~product::rate~' in x ) self.assertTrue( '~product::total~' in x ) self.assertTrue( '~repeatrow|product::desc~' in x )