import win32com.client import os, time __version__ = "$Revision: 0.2$" __author__=["Scott Kirkwood (scott_kirkwood@bigfoot.com)",] __license__ = "GPL Version 2" class OOoObject: def __init__(self): self.serviceManager = win32com.client.Dispatch("com.sun.star.ServiceManager") self.serviceManager._FlagAsMethod("Bridge_GetStruct") self.desktop = self.serviceManager.createInstance("com.sun.star.frame.Desktop") def createStruct(self, strTypeName): return self.serviceManager.Bridge_GetStruct(strTypeName) def createProperty(self, strPropName, strPropVal): """ Create an OOo property name, value object """ prop = self.createStruct("com.sun.star.beans.PropertyValue") prop.Name = strPropName prop.Value = strPropVal return prop def saveAs(self, strFromFileName, strToFileName): """" Save the HTML file as one of DOC, PDF, or TXT using OpenOffice The extension of the strToFileName is used to determine the type of file to output (case sensitive). """ url = strFromFileName # By using HTML (StarWriter) we'll open it with the Writer program instead of the Web one which affects how the PDF is saved openFilter = self.createProperty("FilterName", "HTML (StarWriter)") present = self.desktop.loadComponentFromURL(url, "_blank", 0, [openFilter]) # The style name is HTML as seen in the dialog box printSettings = present.StyleFamilies.getByName("PageStyles").getByName("HTML") # For some reason the left margin is set to 2cm instead of 1cm # This will change it to 1cm printSettings.LeftMargin = 1000 # Set to paper size to A4 printSettings.Size.Width = 2100 printSettings.Size.Height = 2970 strFileName = strToFileName print strFileName props = [] if strFileName[-4:] == ".pdf": saveProperty = self.createProperty("FilterName", "writer_pdf_Export") compressProperty = self.createProperty("CompressionMode", "1") props = [saveProperty, compressProperty] elif strFileName[-4:] == ".doc": saveProperty = self.createProperty("FilterName", "MS Word 97") props = [saveProperty] elif strFileName[-4:] == ".txt": saveProperty = self.createProperty("FilterName", "Text") props = [saveProperty] present.storeToUrl(strFileName, props) present.Close(False) def runXsl(params): (input, out) = os.popen4(' '.join(params)) input.close() while 1: line = out.readline() if not line: break line = line.rstrip() print line def doConvert(params, strInputXml, strOutfile): print "Make %s" % (strOutfile) runXsl(['xsltproc', ' '.join(['--param %s \'"%s"\'' % (key, val) for (key, val) in params.items()]), '--output %s' % strOutfile, 'resume.xsl', strInputXml]) todo = ( 'tests', 'html', 'pdf', 'doc', 'txt' ) nCurYear = int(time.strftime("%Y")) nCurMonth = int(time.strftime("%m")) if 'tests' in todo: from DiffUtils import showDiffs doConvert({'lang' : 'en', 'curyear' : nCurYear, 'curmon' : nCurMonth}, 'test-resume.xml', 'test-resume-en.html') showDiffs('correct-resume-en.html', 'test-resume-en.html') doConvert({'lang' : 'pt', 'curyear' : nCurYear, 'curmon' : nCurMonth}, 'test-resume.xml', 'test-resume-pt.html') showDiffs('correct-resume-pt.html', 'test-resume-pt.html') if 'html' in todo: doConvert({'lang' : 'en', 'curyear' : nCurYear, 'curmon' : nCurMonth}, 'resume.xml', 'resume-en.html') doConvert({'lang' : 'pt', 'curyear' : nCurYear, 'curmon' : nCurMonth}, 'resume.xml', 'resume-pt.html') doConvert({'lang' : 'en', 'curyear' : nCurYear, 'curmon' : nCurMonth, 'gui' : 'no', 'links' : 'no'}, 'resume.xml', 'resume-en-p.html') doConvert({'lang' : 'pt', 'curyear' : nCurYear, 'curmon' : nCurMonth, 'gui' : 'no', 'links' : 'no'}, 'resume.xml', 'resume-pt-p.html') fullPath = os.getcwd() fullPath = "file:///" + fullPath.replace(":", "|").replace("\\", "/") + "/" oooBaby = OOoObject(); if 'pdf' in todo: oooBaby.saveAs(fullPath + 'resume-pt-p.html', fullPath + 'ScottKirkwood-pt.pdf') oooBaby.saveAs(fullPath + 'resume-en-p.html', fullPath + 'ScottKirkwood-en.pdf') if 'doc' in todo: oooBaby.saveAs(fullPath + 'resume-pt-p.html', fullPath + 'ScottKirkwood-pt.doc') oooBaby.saveAs(fullPath + 'resume-en-p.html', fullPath + 'ScottKirkwood-en.doc') if 'txt' in todo: oooBaby.saveAs(fullPath + 'resume-pt-p.html', fullPath + 'ScottKirkwood-pt.txt') oooBaby.saveAs(fullPath + 'resume-en-p.html', fullPath + 'ScottKirkwood-en.txt')