| View previous topic :: View next topic |
| Author |
Message |
abgdf General User


Joined: 04 Jun 2006 Posts: 20 Location: Germany, Europe
|
Posted: Sun Apr 29, 2012 1:11 pm Post subject: [Solved]Setting Tabstops with Python? |
|
|
Hi,
I was trying all day to set a tabstop with Python/PyUno, but I keep failing.
I'm connected to OOo and can insert text and make it 'bold' and stuff (so I got a cursor called "self.cursor").
I can define a tabstop like this:
| Code: | from com.sun.star.style import TabStop
LEFT = uno.Enum("com.sun.star.style.TabAlign", "LEFT")
RIGHT = uno.Enum("com.sun.star.style.TabAlign", "RIGHT")
ts = TabStop()
# Alternatively:
# ts = uno.createUnoStruct("com.sun.star.style.TabStop")
ts.Position = 1300
ts.Alignment = RIGHT |
But how on earth do I get this tabstop into the current paragraph?
I kept trying:
| Code: | | self.cursor.setPropertyValue("ParaTabStops", ts) |
or
| Code: | | self.cursor.setPropertyValue("ParaTabStops", (ts,)) |
(as it seems, these "ParaTabStops" have to be replaced by a new array of structs) but it didn't work at all. I could really use some help at this point.
Documents I tried to get answers from:
1. Easy in Basic.
2. No real answer here.
3. The poster here says, he can do it, but doesn't show how.
Thanks
Last edited by abgdf on Mon Apr 30, 2012 5:23 am; edited 1 time in total |
|
| Back to top |
|
 |
fjcc General User

Joined: 29 Apr 2012 Posts: 5
|
Posted: Sun Apr 29, 2012 8:53 pm Post subject: |
|
|
This seems to work
| Code: | import uno
from com.sun.star.style import TabStop
from com.sun.star.style.TabAlign import RIGHT
def Main():
oDoc = XSCRIPTCONTEXT.getDocument()
oText = oDoc.getText()
oCurs = oText.createTextCursor()
oCurs.gotoStart
ts = TabStop()
ts.Position = 1300
ts.Alignment = RIGHT
oCurs.ParaTabStops = (ts,)
|
|
|
| Back to top |
|
 |
abgdf General User


Joined: 04 Jun 2006 Posts: 20 Location: Germany, Europe
|
Posted: Mon Apr 30, 2012 5:22 am Post subject: |
|
|
Yes, it works!
(I feel a little stupid now. )
Thank you very much! |
|
| Back to top |
|
 |
abgdf General User


Joined: 04 Jun 2006 Posts: 20 Location: Germany, Europe
|
Posted: Mon Apr 30, 2012 7:15 am Post subject: |
|
|
By the way, with your help my scripting-class looks like this now:
| Code: | #!/usr/bin/python
# coding: utf-8
import os
import sys
import uno
from com.sun.star.text.ControlCharacter import PARAGRAPH_BREAK
class OpenOfficeScripter:
def __init__(self):
self.isopen = False
def openOO(self, template = None):
command = 'oowriter --nologo --accept="socket,host=localhost,port=2002;urp;" '
if template:
command += template + " "
command += "&"
os.system(command)
def setOpen(self):
self.isopen = True
def setClosed(self):
self.isopen = False
def checkIsOpen(self):
if self.isopen:
return True
else:
print "Error: OpenOffice/LibreOffice not open."
return False
def establishPyUnoConnection(self):
if not self.checkIsOpen():
return
self.local = uno.getComponentContext()
self.resolver = self.local.ServiceManager.createInstanceWithContext ("com.sun.star.bridge.UnoUrlResolver", self.local)
self.context = self.resolver.resolve("uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext")
self.desktop = self.context.ServiceManager.createInstanceWithContext ("com.sun.star.frame.Desktop", self.context)
self.document = self.desktop.getCurrentComponent()
self.cursor = self.document.Text.createTextCursor()
def gotoBookmark(self, name):
if not self.checkIsOpen():
return
self.bookmark = self.document.Bookmarks.getByName(name)
self.cursor = self.document.Text.createTextCursorByRange(self.bookmark.Anchor)
def insertText(self, text):
if not self.checkIsOpen():
return
self.document.Text.insertString(self.cursor, text, 0)
def setBold(self):
if not self.checkIsOpen():
return
self.cursor.setPropertyValue("CharWeight", 150)
def setItalic(self):
if not self.checkIsOpen():
return
cursor.setPropertyValue("CharPosture", 1)
def setUnderlined(self, mode = "single"):
if not self.checkIsOpen():
return
modes = ("single", "double")
self.cursor.setPropertyValue("CharUnderline", modes.index(mode) + 1)
def setNormalText(self):
if not self.checkIsOpen():
return
self.cursor.setPropertyValue("CharWeight", 100)
self.cursor.setPropertyValue("CharPosture", 0)
self.cursor.setPropertyValue("CharUnderline", 0)
def setFont(self, fontname):
if not self.checkIsOpen():
return
cursor.setPropertyValue("CharFontName", fontname)
def typeReturn(self):
if not self.checkIsOpen():
return
self.document.Text.insertControlCharacter(self.cursor, PARAGRAPH_BREAK, 0)
def insertPageBreak(self):
if not self.checkIsOpen():
return
self.cursor.setPropertyValue("BreakType", 5)
def typeBackspace(self):
if not self.checkIsOpen():
return
self.cursor.goLeft(1,1)
self.cursor.setString("")
def insertIntoTextFrame(self, name, text):
if not self.checkIsOpen():
return
self.frames = self.document.getTextFrames()
self.frame = self.frames.getByName(name)
self.cursor = self.frame.Text.createTextCursor()
self.frame.Text.insertString(self.cursor, text, 0)
def redefineTabstops(self, tabstops = ((12, "left"),)):
if not self.checkIsOpen():
return
# "tabstops" is a tuple of tuples. The inner tuples contain
# two values: The tabstop-position in centimeters and the
# alignment-string as shown in "alignments" right below.
alignments = ("left", "centered", "right", "decimal")
tabs = []
for i in range(len(tabstops)):
if len(tabstops[i]) != 2 or not str(tabstops[i][0]).isdigit() or tabstops[i][1] not in alignments:
print "Error: Tabstop not defined correctly (by position and alignment)."
sys.exit(1)
ts = uno.createUnoStruct("com.sun.star.style.TabStop")
ts.Position = int(tabstops[i][0] * 1000)
ts.Alignment = alignments.index(tabstops[i][1])
tabs.append(ts)
self.cursor.ParaTabStops = tuple(tabs)
def printParagraphStyles(self):
if not self.checkIsOpen():
return
a = self.document.getStyleFamilies()
b = a.getByName("ParagraphStyles")
for i in b.getElementNames():
print i
def printCursorProperties(self):
if not self.checkIsOpen():
return
for i in self.cursor.getPropertySetInfo().getProperties():
print i.Name
def newDoc(self):
if not self.isopen:
return "Error: OpenOffice not open."
self.document = self.desktop.loadComponentFromURL( "private:factory/swriter", "_blank", 0, () )
self.cursor = self.document.Text.createTextCursor()
def loadDoc(self,url):
if not self.isopen:
return "Error: OpenOffice not open."
self.document = self.desktop.loadComponentFromURL(url, "_blank", 0, ())
self.cursor = self.document.Text.createTextCursor() |
It gets larger, the more functions of PyUno I discover.
The user has to call ".setOpen()" by hand after OOo is opened properly. ".openOO()" can be called to open it, but it has to be checked, that this process is finished already. After that, you can call ".establishPyUnoConnection()" and then start scripting.
Cheers! |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|