Convert text in {} to paragraph style

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
vector
Posts: 7
Joined: Wed Jun 13, 2018 3:32 am

Convert text in {} to paragraph style

Post by vector »

Hi All,
is it possible(and how easy) is to find a line like this

Code: Select all

{title: In Times of Trial}
and remove the brackets and title tag to just leave the text
In Times of Trial as a titlestyle

Id also like to treat

Code: Select all

{subtitle:Traditional}
{c:Verse1}
in a similar manner leaving me with just the text
Traditional as a subtitle style
and Verse1 as a callout style

I can do this manually with a number of F&R and clicks etc but would be nice to have it as a macro? perhaps
I am not wise in the ways of such things and have many files to convert/format. So would appreciate any help.

I would like to simply open the text file with open office, run the macro and apply some formatting and bold/italics etc to the now specified styles.
open Office 4 Windows/Ubuntu
User avatar
Zizi64
Volunteer
Posts: 11352
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Convert text in {} to paragraph style

Post by Zizi64 »

but would be nice to have it as a macro?
I think: You must write it. We can help you.

Otherwise you can try the AltSearch extension:
https://extensions.libreoffice.org/exte ... for-writer
https://extensions.openoffice.org/en/pr ... -altsearch
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
vector
Posts: 7
Joined: Wed Jun 13, 2018 3:32 am

Re: Convert text in {} to paragraph style

Post by vector »

thx ill look into those links and get back to you
open Office 4 Windows/Ubuntu
User avatar
RoryOF
Moderator
Posts: 34586
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Convert text in {} to paragraph style

Post by RoryOF »

The AltSearch extension should do it.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: Convert text in {} to paragraph style

Post by JeJe »

This is easy to do. Adapt this paragraph enumerator from "Useful Macro Information For OpenOffice" By Andrew Pitonyak (which is a free download and you should read)

You just need the Instr function to look at the start of the paragraph string and set the paragraph stylename accordingly and remove the bits at the beginning and end.

Code: Select all

Sub EnumerateParagraphs
REM Author: Andrew Pitonyak
Dim oParEnum 'Enumerator used to enumerate the paragraphs
Dim oPar 'The enumerated paragraph
REM Enumerate the paragraphs.
REM Tables are enumerated along with paragraphs
oParEnum = ThisComponent.getText().createEnumeration()
Do While oParEnum.hasMoreElements()
oPar = oParEnum.nextElement()
REM This avoids the tables. Add an else statement if you want to
REM process the tables.
If oPar.supportsService("com.sun.star.text.Paragraph") Then
MsgBox oPar.getString(), 0, "I found a paragraph"
ElseIf oPar.supportsService("com.sun.star.text.TextTable") Then
Print "I found a TextTable"
Else
Print "What did I find?"
End If
Loop
End Sub

Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
Sébastien C
Posts: 111
Joined: Mon Jan 04, 2010 5:06 pm
Location: Meymac, France

Re: Convert text in {} to paragraph style

Post by Sébastien C »

If AltSearch extension should do it, it not change the fact to open a dialog box and give the instructions each times in more of ONE command.

But search and replace with a regular expression with macro is easy... We have seen that recently.

Title, subtitle and callout styles are all paragraph styles. The treatment is therefore the same for this type of styles.

Code: Select all

Sub seachReplaceStyles()
	Dim   mySearch As Object,    myResult As Object, myElement As Object
	Dim myString() As String, myStyleName As String
	Dim          i As Integer

	mySearch = thisComponent.createSearchDescriptor()

	mySearch.searchString            = "\{.{1,}:.{1,}\}"                     ' The string that you search.
	mySearch.searchRegularExpression = True

	myResult = thisComponent.findAll(mySearch)
       
	If myResult.hasElements Then
	 For i = 0 to myResult.count-1
	                   myElement = myResult.getByIndex(i)
	                    myString = split(myElement.string, ":")
	                 myStyleName = right(myString(0), len(myString(0)) - 1)
	                 myString(0) = ""
	  myString(uBound(myString)) = left(myString(uBound(myString)), len(myString(uBound(myString))) - 1)
	            myElement.string = join(myString, "")
	     myElement.ParaStyleName = myStyleName                               ' The style that you apply to your string.
     Next i
    End If
End Sub
 Edit:  

If you want to delete the possible space(s) in left and in right of the name's style, you can replace the line

Code: Select all

myElement.ParaStyleName = myStyleName                               ' The style that you apply to your string.
by

Code: Select all

myElement.ParaStyleName = Trim(myStyleName)                         ' The style that you apply to your string.
 
:D
Attachments
seachReplaceStyles.odt
(17.08 KiB) Downloaded 139 times
LibreOffice v. 7.3.2.2, under GNU-Linux Mint and, in virtualization and just for tests, LibreOffice v. 7.3.2.2 an OpenOffice v. 4.1.12 under M$-W 10 :ouch: .
User avatar
Lupp
Volunteer
Posts: 3542
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: Convert text in {} to paragraph style

Post by Lupp »

@"vector":
Your question is containing the phrases "a titlestyle", "a subtitle style" and "a callout style".
I feel unsure:
-1- Do you want to assign a specific paragraph style in each case to the complete paragraph?
-2- For what reason do you include the textcontent in the curly brackets? What if there are additional characters? Shall they be removed?
-3a- How is the specific stylename in each case assigned? Is it supposed to be the string between "{" and ":" as Sébastien assumed?
-3b- Or do you need a way to assign stylenames to the "{somelabel:" parts in an additional step?
-3- Is the different usage of a space in the cases an accident?

@Sébastien C:
-1- You searched for text parts by RegEx without excluding positions anywhere inside a paragraph. A string like "...some text {mySubtitle: and} another text..." anywhere in a paragraph would also trigger the style assignment. The different placement of text parts in-/outside of the curly brackets would be accepted as meaning nothing.
-2- Comments on parts of a quotation as sometimes done in German like in "there are not brackets {orth: Lupp}" would cause a script error.
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: Convert text in {} to paragraph style

Post by JeJe »

Okay... here's one way to do my solution... it assumes the paragraphs are all plain text and the styles are already in the document.

Code: Select all


Sub EnumerateParagraphs
	MODIFIED FROM REM Author: Andrew Pitonyak
	Dim oParEnum 'Enumerator used to enumerate the paragraphs
	Dim oPar 'The enumerated paragraph
	REM Enumerate the paragraphs.
	REM Tables are enumerated along with paragraphs
	oParEnum = ThisComponent.getText().createEnumeration()
	Do While oParEnum.hasMoreElements()
		oPar = oParEnum.nextElement()
		REM This avoids the tables. Add an else statement if you want to
		REM process the tables.
		If oPar.supportsService("com.sun.star.text.Paragraph") Then
			st= TRIM(oPar.getString())
			if left(st,1) = "{" and right(st,1) ="}" then
				a= instr(1,st,":")
				if a then
					stylename = trim(mid(st,2,a-2))


					if stylename = "c" then stylename = "callout"

					st=TRIM(mid(st,a+1,len(st) - a-1))
					oPar.parastylename = stylename
					oPar.string = st

				end if
			end if

			'ElseIf oPar.supportsService("com.sun.star.text.TextTable") Then
			'Print "I found a TextTable"
			'Else
			'Print "What did I find?"
		End If
Loop
End Sub


Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
vector
Posts: 7
Joined: Wed Jun 13, 2018 3:32 am

Re: Convert text in {} to paragraph style

Post by vector »

wow,
ok give me some time to try and understand these replies and ill get back to you.
Very much appreciated
open Office 4 Windows/Ubuntu
vector
Posts: 7
Joined: Wed Jun 13, 2018 3:32 am

Re: Convert text in {} to paragraph style

Post by vector »

I am overwhelmed by the amzing help out here!
many thanks to all
Kai who created the chordprotransposer got in touch with me and has also created a new extension for me , well for chordpro users ;) doing just this.
Kai and I are currently testing the extension Ill let you know if he releases it public. So far it working fantastic.


https://extensions.libreoffice.org/exte ... oser/1-0.6
open Office 4 Windows/Ubuntu
User avatar
Hagar Delest
Moderator
Posts: 32627
Joined: Sun Oct 07, 2007 9:07 pm
Location: France

Re: Convert text in {} to paragraph style

Post by Hagar Delest »

I remember that one also: [Solved] Taking note and tagging format.
LibreOffice 7.6.2.1 on Xubuntu 23.10 and 7.6.4.1 portable on Windows 10
Post Reply