[Solved] Change paragraph properties

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
Grytpype
Posts: 11
Joined: Mon Apr 14, 2014 5:26 pm
Location: London

[Solved] Change paragraph properties

Post by Grytpype »

I want to change the line spacing of paragraphs. I am a beginner at LibreOffice macro writing, and this mostly useful document https://wiki.openoffice.org/wiki/Docume ... _Documents seems to say that paragraph properties work pretty much the same as character properties. But it has no actual examples. When I enumerate paragraphs, the character properties change correctly, but the corresponding paragraph properties don't change (though the assignment is syntactically correct).

Code: Select all

doc = thiscomponent
enum = doc.text.createenumeration
while enum.hasmoreelements
	elem = enum.nextelement
	if elem.supportsservice("com.sun.star.text.Paragraph") then
		elem.charfontname = "Arial"
		elem.charheight = 12
		elem.paralinespacing.height = 150
		elem.parabackcolor(rgb(200,200,0))
	end if
wend
So now the paragraphs are Arial 12 but their lines are not 1.5 lines apart and the background is still plain old white. How do I change the para properties? I've tried every permutation I can think of - using LineSpacing and SetPropertyValue and whatnot. Of course I can record a macro that starts with Select All and that shows me that it uses .uno:LineSpacing, Name = LineSpacing.Height, but I don't know how to turn that into proper Basic as above. Thank you.
Last edited by Grytpype on Mon Apr 14, 2014 6:29 pm, edited 1 time in total.
LibreOffice 6.2.0.3 on Windows 10 Pro, as of March 2019: just upgraded from 4.1.6.2
FJCC
Moderator
Posts: 9577
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Change paragraph properties

Post by FJCC »

ParaLineSpacing is a structure, so it must be copied, modified and then replaced as a whole. The RGB() function returns a value which you assign to the ParaBackColor property.

Code: Select all

doc = thiscomponent
enum = doc.text.createenumeration
while enum.hasmoreelements
   elem = enum.nextelement
   if elem.supportsservice("com.sun.star.text.Paragraph") then
      elem.charfontname = "Arial"
      elem.charheight = 12
      LineSpace = elem.paralinespacing
      LineSpace.height = 150
      elem.paralinespacing = LineSpace
      elem.parabackcolor = rgb(200,200,0)
   end if
wend
Rather than create a macro, wouldn't it be easier just to modify the Paragraph styles of the document? Even in a macro it looks cleaner. The code below assumes you are using the Default paragraph style.

Code: Select all

Sub EditStyle
oStyleFamilies = ThisComponent.getStyleFamilies()
oPStyles = oStyleFamilies.getByName("ParagraphStyles")
DefaultStyle = oPStyles.getByName("Standard")
LineSpace = DefaultStyle.paralinespacing
LineSpace.height = 150
DefaultStyle.paralinespacing = LineSpace
DefaultStyle.parabackcolor = rgb(200,200,0)
End Sub
All of that can be done with a few clicks in the Styles and Formatting dialog ( menu Format -> Styles and Formatting)
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
Grytpype
Posts: 11
Joined: Mon Apr 14, 2014 5:26 pm
Location: London

Re: Change paragraph properties

Post by Grytpype »

Thank you - that's just what I want, and very quick. So it's not a general problem with them, it's individual problems. (The colour was only there to give me a quicker visual for whether my spacing test worked.) Structs are a bit more advanced than I usually use, so I didn't know I couldn't just assign inside it with another dot. (Which would seem a sensible way of doing it.)

My toy example can of course be replaced by a Select All, but in reality I want to do this differently for individual paragraphs.
LibreOffice 6.2.0.3 on Windows 10 Pro, as of March 2019: just upgraded from 4.1.6.2
Post Reply