I working with python with 2 pieces of RTF validated content.
I can put the content on body, but I don't know how change the Header programmaticaly by python.
Somebody can help me with this?
Thanks in advance.
Gabriel
Code: Select all
import uno
import unohelper
import string
import sys
import StringIO
import os
from com.sun.star.beans import PropertyValue
from unohelper import Base
from com.sun.star.io import IOException, XOutputStream, XInputStream, XSeekable
currDir = os.path.dirname( os.path.realpath(__file__) )
# Se obtiene el documento por stdin.
input = ""
input_pie = ""
input_nada = ""
input_cuerpo = ""
input_cabecera = ""
comenzo_cuerpo=0
input = ""
for line in sys.stdin:
input = input + line
paso=1
lista = input.split("SEPARADOR")
for parte in lista:
if paso==1:
input_nada = parte
if paso==2:
input_cabecera = input_nada+parte+'}'
if paso==3:
input_cuerpo = '{\\rtf1\\ansi\\ansicpg1252\\deff0{\\fonttbl{\\f0\\fswiss\\fcharset0 Calibri;}}{\\*\\generator Msftedit 5.41.21.2509;}'+parte+''
if paso==4:
input_pie = parte
paso=paso+1
input_Stream = StringIO.StringIO(input)
input_nada_Stream = StringIO.StringIO(input_nada)
input_cabecera_Stream = StringIO.StringIO(input_cabecera)
input_cuerpo_Stream = StringIO.StringIO(input_cuerpo)
input_pie_Stream = StringIO.StringIO(input_pie)
#Setup config
localContext = uno.getComponentContext()
resolver = localContext.ServiceManager.createInstanceWithContext(
"com.sun.star.bridge.UnoUrlResolver", localContext )
ctx = resolver.resolve( "uno:socket,host=127.0.0.1,port=8100;urp;StarOffice.ComponentContext" )
smgr = ctx.ServiceManager
desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx)
document = desktop.loadComponentFromURL("file://" + currDir + "/plantilla-original.ott", "_blank", 0, ())
text = document.Text
cursor = text.createTextCursor()
class InputStream(unohelper.Base, XInputStream, XSeekable):
""" Minimal Implementation of XInputStream """
def __init__(self, inStream):
self.stream = inStream
self.stream.seek(0, os.SEEK_END)
self.size = self.stream.tell()
def readBytes(self, retSeq, nByteCount):
retSeq = self.stream.read(nByteCount)
return (len(retSeq), uno.ByteSequence(retSeq))
def readSomeBytes(self, foo, n):
return self.readBytes(foo, n)
def skipBytes(self, n):
self.stream.seek (n, 1)
def available(self):
return self.size - self.stream.tell();
def closeInput(self):
self.stream.close()
def seek(self, posn):
self.stream.seek(int(posn))
def getPosition(self):
return long(self.stream.tell())
def getLength(self):
return long(self.size)
cursor.gotoHeader(False)
inPropsc = (
PropertyValue( "FilterName" , 0, "Rich Text Format" , 0 ),
PropertyValue( "InputStream", 0, InputStream(input_cabecera_Stream), 0)
)
cursor.insertDocumentFromURL("private:stream", inPropsc)
cursor.gotoEnd(True)
inProps1 = (
PropertyValue( "FilterName" , 0, "Rich Text Format" , 0 ),
PropertyValue( "InputStream", 0, InputStream(input_cuerpo_Stream), 0)
)
cursor.insertDocumentFromURL("private:stream", inProps1)
cursor.gotoEnd(False)
cursor.gotoStart(False)
replace = document.createReplaceDescriptor()
def search(aReplace, _from, _to ):
aReplace.SearchString = _from
aReplace.ReplaceString = _to
document.replaceAll(aReplace)
return None
class OutputStream( Base, XOutputStream ):
def __init__( self ):
self.closed = 0
def closeOutput(self):
self.closed = 1
def writeBytes( self, seq ):
sys.stdout.write( seq.value )
def flush( self ):
pass
filterName = "writer_pdf_Export"
outProps = (
PropertyValue( "FilterName" , 0, filterName , 0 ),
PropertyValue( "Overwrite" , 0, True , 0 ),
PropertyValue( "OutputStream", 0, OutputStream(), 0)
)
try:
document.storeToURL("private:stream", outProps)
except IOException, e:
sys.stderr.write("Error: " + e.Message)
document.dispose()