Convert text in {} to paragraph style

Creating a macro - Writing a Script - Using the API

Convert text in {} to paragraph style

Postby vector » Wed Jun 13, 2018 4:28 am

Hi All,
is it possible(and how easy) is to find a line like this
Code: Select all   Expand viewCollapse view
{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   Expand viewCollapse view
{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
vector
 
Posts: 7
Joined: Wed Jun 13, 2018 3:32 am

Re: Convert text in {} to paragraph style

Postby Zizi64 » Wed Jun 13, 2018 7:59 am

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; LO4.4.7, LO5.4.7 on Win7x64Prof.
PortableApps, winPenPack: LO3.3.0-LO6.0.4 and AOO4.1.5
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.
User avatar
Zizi64
Volunteer
 
Posts: 6714
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Convert text in {} to paragraph style

Postby vector » Wed Jun 13, 2018 8:16 am

thx ill look into those links and get back to you
open Office 4 Windows/Ubuntu
vector
 
Posts: 7
Joined: Wed Jun 13, 2018 3:32 am

Re: Convert text in {} to paragraph style

Postby RoryOF » Wed Jun 13, 2018 8:41 am

The AltSearch extension should do it.
Apache OpenOffice 4.1.5 on Xubuntu 18.04 (mostly 64 bit version) and infrequently on Win2K/XP
14 October 2016 was Pooh's 90th birthday
User avatar
RoryOF
Moderator
 
Posts: 26693
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Convert text in {} to paragraph style

Postby JeJe » Wed Jun 13, 2018 2:06 pm

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   Expand viewCollapse view
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

Openoffice 4.1.2
Windows 8
JeJe
 
Posts: 221
Joined: Wed Mar 09, 2016 2:40 pm

Re: Convert text in {} to paragraph style

Postby Sébastien C » Wed Jun 13, 2018 3:28 pm

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   Expand viewCollapse view
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   Expand viewCollapse view
myElement.ParaStyleName = myStyleName                               ' The style that you apply to your string.
by
Code: Select all   Expand viewCollapse view
myElement.ParaStyleName = Trim(myStyleName)                         ' The style that you apply to your string.
 

:D
Attachments
seachReplaceStyles.odt
(17.08 KiB) Downloaded 7 times
LibreOffice 5.x under GNU-Linux Mageia 5.0 and M$-W XP
User avatar
Sébastien C
 
Posts: 56
Joined: Mon Jan 04, 2010 5:06 pm
Location: Meymac, France

Re: Convert text in {} to paragraph style

Postby Lupp » Wed Jun 13, 2018 10:17 pm

@"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 5.4.4 and older versions, PortableOpenOffice 4.1.5 and older, StarOffice 5.2
---
Maybe we might! (Create a powerful UFO: United Free Office)
Lupp from München
User avatar
Lupp
Volunteer
 
Posts: 1824
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: Convert text in {} to paragraph style

Postby JeJe » Wed Jun 13, 2018 11:24 pm

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   Expand viewCollapse view

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


Openoffice 4.1.2
Windows 8
JeJe
 
Posts: 221
Joined: Wed Mar 09, 2016 2:40 pm

Re: Convert text in {} to paragraph style

Postby vector » Fri Jun 15, 2018 2:49 am

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

Postby vector » Mon Jun 18, 2018 1:02 am

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
vector
 
Posts: 7
Joined: Wed Jun 13, 2018 3:32 am

Re: Convert text in {} to paragraph style

Postby Hagar Delest » Mon Jun 18, 2018 8:05 am

I remember that one also: [Solved] Taking note and tagging format.
AOO 4.1.5 on Xubuntu 18.04 and on Windows 7 (with winPenPack port).
User avatar
Hagar Delest
Moderator
 
Posts: 28074
Joined: Sun Oct 07, 2007 9:07 pm
Location: France


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 5 guests