import uno #copy>pasted, not checked for 'smaller' errors
import os
def study() :
D = writer_print()
D.writeLine("adfdsa",3.89891)
ctx = uno.getComponentContext()
smgr = ctx.ServiceManager
StarDeskTop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx)
dispatcher = smgr.createInstanceWithContext( "com.sun.star.frame.DispatchHelper",ctx )
com_sun_star_text_ControlCharacter_PARAGRAPH_BREAK = uno.getConstantByName( "com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK" )
com_sun_star_text_ControlCharacter_LINE_BREAK = uno.getConstantByName( "com.sun.star.text.ControlCharacter.LINE_BREAK" )
com_sun_star_text_ControlCharacter_HARD_HYPHEN = uno.getConstantByName( "com.sun.star.text.ControlCharacter.HARD_HYPHEN" )
com_sun_star_text_ControlCharacter_SOFT_HYPHEN = uno.getConstantByName( "com.sun.star.text.ControlCharacter.SOFT_HYPHEN" )
com_sun_star_text_ControlCharacter_HARD_SPACE = uno.getConstantByName( "com.sun.star.text.ControlCharacter.HARD_SPACE" )
com_sun_star_text_ControlCharacter_APPEND_PARAGRAPH = uno.getConstantByName( "com.sun.star.text.ControlCharacter.APPEND_PARAGRAPH" )
from com.sun.star.beans import PropertyValue
class writer_print():
"""A class which allows conveniently printing stuff into a Writer document.
Very useful for debugging, general output purposes, or even for creating reports.
Adapted from https://wiki.openoffice.org/wiki/PrinttoWriter.py"""
def __init__( self ):
desktop = smgr.createInstanceWithContext('com.sun.star.frame.Desktop', ctx)
for doc in desktop.getComponents():
if doc.Title=="print.odt":
break
else:
pathSettings = smgr.createInstanceWithContext( "com.sun.star.util.PathSettings",ctx )
i = pathSettings.Work.find("//")
cwd = ( pathSettings.Work )[i+2:] # get user work directory in OS terms
name = uno.systemPathToFileUrl( os.path.join( cwd , "print.odt") )
doc = StarDeskTop.loadComponentFromURL( name , "_blank", 0,() )
win = doc.getCurrentController().getFrame().getComponentWindow()
win.setFocus()
self.oWriterDoc = doc
self.oWriterText = self.oWriterDoc.getText()
self.oWriterCursor = self.oWriterText.createTextCursor()
self.oWriterCursor.Start
def writeLine( self, *args ):
if len( args ) > 0:
self.write( *args )
self.writeParagraphBreak()
def write( self, arg1, *argsRest ):
self.writeParagraphBreak()
self.writeOne( arg1 )
for arg in argsRest:
self.writeTab()
self.writeOne( arg )
def writeOne( self, arg, bAbsorb=False ):
if type(arg) == list:
expand = []
for el in arg:
if type(el) != str:
el = round(el,2)
expand.append(el)
out = ""
for el in expand:
out += str(el) + ","
arg = out
elif type(arg) != str:
arg = arg #round(arg,2)
self.writeString( " " + str( arg ), bAbsorb )
def writeTab( self, bAbsorb=False ):
self.writeString( "\t", bAbsorb )
def writeParagraphBreak( self, bAbsorb=False ):
self.writeControlCharacter( com_sun_star_text_ControlCharacter_PARAGRAPH_BREAK, bAbsorb )
# self.writeControlCharacter( com_sun_star_text_ControlCharacter_LINE_BREAK, bAbsorb )
def writeString( self, cString, bAbsorb=False ):
self.oWriterText.insertString( self.oWriterCursor, cString, bAbsorb )
def writeControlCharacter( self, nCtrlChar, bAbsorb=False ):
self.oWriterText.insertControlCharacter( self.oWriterCursor, nCtrlChar, bAbsorb )
def writeEnd(self,character="="):
self.oWriterCursor.End
self.writeParagraphBreak()
self.writeString( 60*character+"<")
def mri( target):
mri = ctx.ServiceManager.createInstanceWithContext( "mytools.Mri",ctx)
mri.inspect(target)
def calc():
fLoad = uno.systemPathToFileUrl( "file://...." , fileTemplate) )
document = StarDeskTop.loadComponentFromURL( fLoad , "_blank", 0, () )
frame = document.getSheets().getByIndex(0)
mri(frame)
cell = frame.getCellByPosition(0,0) # = A1
cellByName = cell.getString() # the contents of cell A1