[Solved] Clearing direct format (Pyuno)

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
imr_lor
Posts: 23
Joined: Fri Sep 04, 2015 10:28 am

[Solved] Clearing direct format (Pyuno)

Post by imr_lor »

Hello everyone
im processing an document and there i have to clear the direct formatting via my programm. My problem is that i cannot find an option to do this in the UNO API.
Maybe you know a way to solve this.
Thx imr_lor
Last edited by imr_lor on Thu Oct 15, 2015 3:44 pm, edited 4 times in total.
OpenOffice 3.1 on LinuxMint 17.1
B Marcelly
Volunteer
Posts: 1160
Joined: Mon Oct 08, 2007 1:26 am
Location: France, Paris area

Re: Clearing direct format

Post by B Marcelly »

Use the dispatcher.
Bernard

OpenOffice.org 1.1.5 / Apache OpenOffice 4.1.1 / LibreOffice 5.0.5
MS-Windows 7 Home SP1
imr_lor
Posts: 23
Joined: Fri Sep 04, 2015 10:28 am

Re: Clearing direct format

Post by imr_lor »

B Marcelly wrote:Use the dispatcher.
ok how do i do this?
The problem is that somehow something that should be bold (im using the style Header2 which i defined as bold) is not bold and i can manually clear Direct format to fix this but i have to do this automatically
Im using Python for this programm

Edit: Thx very much for the tip(Dispatcher) now i only gonna have to figure out how to use this in Python
OpenOffice 3.1 on LinuxMint 17.1
User avatar
RoryOF
Moderator
Posts: 34586
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Clearing direct format

Post by RoryOF »

I'd do this directly. Find All Heading 2 using Styles. All Heading 2s will be selected. The press Ctrl M, or select Clear Formatting from the format dropdown.

Try this on a copy file first - why bother with a macro?
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
imr_lor
Posts: 23
Joined: Fri Sep 04, 2015 10:28 am

Re: Clearing direct format

Post by imr_lor »

RoryOF wrote:I'd do this directly. Find All Heading 2 using Styles. All Heading 2s will be selected. The press Ctrl M, or select Clear Formatting from the format dropdown.

Try this on a copy file first - why bother with a macro?
I have to do this because this is an automatically generated document which is generated based on an input from an JSON file.
The Generated Document has to use specific Styles and formats i.e. the headings should Arial 11 and bold.
Because of an annoying (and not fixable bug) with the input string of the heading it gets the direct format of not being bold.
So fixing it manually wont be as good as needed because i wont be the one to use this Programm.
I have to fix this before the User opens the Document.
OpenOffice 3.1 on LinuxMint 17.1
B Marcelly
Volunteer
Posts: 1160
Joined: Mon Oct 08, 2007 1:26 am
Location: France, Paris area

Re: Clearing direct format

Post by B Marcelly »

Thx very much for the tip(Dispatcher) now i only gonna have to figure out how to use this in Python
If you don't know how to translate this in Python, stick to Basic.

Code: Select all

document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
dispatcher.executeDispatch(document, ".uno:ResetAttributes", "", 0, Array())
Bernard

OpenOffice.org 1.1.5 / Apache OpenOffice 4.1.1 / LibreOffice 5.0.5
MS-Windows 7 Home SP1
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Clearing direct format

Post by Villeroy »

B Marcelly wrote:
Thx very much for the tip(Dispatcher) now i only gonna have to figure out how to use this in Python
If you don't know how to translate this in Python, stick to Basic.

Code: Select all

document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
dispatcher.executeDispatch(document, ".uno:ResetAttributes", "", 0, Array())

Code: Select all

ThisComponent = XSCRIPTCONTEXT.getDocument()
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
dispatcher.executeDispatch(document, ".uno:ResetAttributes", "", 0, ())
Well, I use Ctrl+A,M
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
imr_lor
Posts: 23
Joined: Fri Sep 04, 2015 10:28 am

Re: Clearing direct format

Post by imr_lor »

ok its now working totally fine to clear the direct format. Thx Guys.
No only one little problem remains:
By now its is clearing the direct format in the whole document which is definetly not intended.
is it possible to clear only the content of an specific style?
or maybe u know an diffrent way to do this?

Code: Select all

def do_XXXXXXX(self):
        if self.XXXXXXXXXX('XXX'):
            self.doHeading('XXXXXXXXXXXXXXXX XXXXXXXXXX  XXXXX', "Heading 2")
            self.addParagraphBreak()

Code: Select all

def doHeading(self, titel, paraStyle, numberingLevel=1):
        self.cl_ID += 1
        doc = self.edit_doc
        cursor = doc.Text.createTextCursor()
        cursor.gotoEnd(False)
        cursor.ParaStyleName = paraStyle
        cursor.NumberingLevel = numberingLevel
        cursor.ListId = self.cl_ID
        if paraStyle == 'Heading 3':
            doc.Text.insertString(cursor, titel, 0)
        else:
            doc.Text.insertString(cursor, '\t' + titel, 0)
        doc.Text.insertControlCharacter(cursor, PARAGRAPH_BREAK, 0)
OpenOffice 3.1 on LinuxMint 17.1
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Clearing direct format (Pyuno)

Post by Villeroy »

Edit>Find&Replace...
[More Options]
[X] Search for style
[Find All]
Ctrl+M
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
imr_lor
Posts: 23
Joined: Fri Sep 04, 2015 10:28 am

Re: Clearing direct format (Pyuno)

Post by imr_lor »

Im sorry but this wont help me in any way.
1. as I explained above this is something that needs to happen in the code. (this post is in the Macros and UNO API Forum for a reason)
2. it is not working as intended.
but thanks for the advice :)
OpenOffice 3.1 on LinuxMint 17.1
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Clearing direct format (Pyuno)

Post by Villeroy »

imr_lor wrote:Im sorry but this wont help me in any way.
1. as I explained above this is something that needs to happen in the code. (this post is in the Macros and UNO API Forum for a reason)
2. it is not working as intended.
but thanks for the advice :)
You want to clear direct formatting within text ranges of a certain paragraph style? The outlined route does exactly this within seconds and it can be written as a macro.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
imr_lor
Posts: 23
Joined: Fri Sep 04, 2015 10:28 am

Re: Clearing direct format (Pyuno)

Post by imr_lor »

Above I showed some of my code.
The second example shows the function which inserts a new headline into the document.
The problem is that sometimes this headline is not bold(but its style is defined as bold) and to fix this i have to remove the direct formatting of the headline in this function.
By now I am stuck at a Point where i always get import errors:

Code: Select all

from com.sun.star.frame import DispatchHelper
gives the error:
ImportError: No module named 'com' (or 'com.sun.star.frame.DispatchHelper' is unknown)

but these imports are working totally fine:

Code: Select all

from com.sun.star.beans import PropertyValue
from com.sun.star.text.ControlCharacter import PARAGRAPH_BREAK, LINE_BREAK, HARD_SPACE
from com.sun.star.style.BreakType import PAGE_BEFORE, PAGE_AFTER
from com.sun.star.style.ParagraphAdjust import CENTER, LEFT, RIGHT
from com.sun.star.lang import XMain 
from com.sun.star.table.CellVertJustify import CENTER as VCENTER
from com.sun.star.awt.FontWeight import BOLD as FW_BOLD
from com.sun.star.awt.FontSlant import ITALIC as FS_ITALIC
from com.sun.star.awt.FontSlant import NONE as FS_NONE
from com.sun.star.awt.FontWeight import NORMAL as FW_NORMAL
[/code ]

Thanks for the effort you are putting in to this. It really helps me a lot.
OpenOffice 3.1 on LinuxMint 17.1
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Clearing direct format (Pyuno)

Post by Villeroy »

Learn Python programming. The DispatchHelper is an object derived from a frame. You can not import any DispatchHelper
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
imr_lor
Posts: 23
Joined: Fri Sep 04, 2015 10:28 am

Re: Clearing direct format (Pyuno)

Post by imr_lor »

Mh ok.
Is the needed module implemented in UNO(PYUNO)?
OpenOffice 3.1 on LinuxMint 17.1
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Clearing direct format (Pyuno)

Post by Villeroy »

imr_lor wrote:Mh ok.
Is the needed module implemented in UNO(PYUNO)?
Sorry, the DHelper is not derived from any frame. In Basic it is created from scratch using the createUnoService(service_name) function. I have a Python class for this helper stuff.

Code: Select all

import uno

gStyle = 'Heading 3'

class OfficeHelper:
    '''Frequently used methods in office context'''
    def __init__(self, ctx=uno.getComponentContext()):
        self.ctx = ctx
        self.smgr = self.ctx.ServiceManager
        
    def createUnoService(self, service):
        return self.smgr.createInstance(service)

    def getDesktop(self):
        return self.smgr.createInstanceWithContext("com.sun.star.frame.Desktop",self.ctx)

    def getCurrentComponent(self):
        return self.getDesktop().getCurrentComponent()

    def getCurrentFrame(self):
        return self.getDesktop().getCurrentFrame()

    def getCurrentComponentWindow(self):
        return self.getCurrentFrame().getComponentWindow()

    def getCurrentContainerWindow(self):
        return self.getCurrentFrame().getContainerWindow()

    def getCurrentController(self):
        return self.getCurrentFrame().getController()

    def callMRI(self,obj=None):
        '''Create an instance of MRI inspector and inspect the given object (default is selection)'''
        if not obj:
            obj = self.getCurrentController().getSelection()
        mri = self.createUnoService("mytools.Mri")
        mri.inspect(obj)

def cleanParaStyle():
    ohelper = OfficeHelper()
    view = ohelper.getCurrentController()
    frame = view.getFrame()
    doc = view.getModel()
    dispatcher = ohelper.createUnoService("com.sun.star.frame.DispatchHelper")
    oFind = findParaStyle(doc, gStyle)
    view.select(oFind)
    dispatcher.executeDispatch(frame, ".uno:ResetAttributes", "", 0, ())
    
def findParaStyle(obj, s):
    oDesc = obj.createSearchDescriptor()
    oDesc.SearchAll = True
    oDesc.setSearchString(s)
    oDesc.SearchStyles = True
    return obj.findAll(oDesc)
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
imr_lor
Posts: 23
Joined: Fri Sep 04, 2015 10:28 am

Re: Clearing direct format (Pyuno)

Post by imr_lor »

Thanks a lot Villeroy I will implement and test this now and if it works this topic will be finnally solved
OpenOffice 3.1 on LinuxMint 17.1
imr_lor
Posts: 23
Joined: Fri Sep 04, 2015 10:28 am

Re: Clearing direct format (Pyuno)

Post by imr_lor »

I modifed the findParaStyle so it only searches for the last

It seems the call getCurrentContoller produces an erro(exit code 139)
OpenOffice 3.1 on LinuxMint 17.1
imr_lor
Posts: 23
Joined: Fri Sep 04, 2015 10:28 am

Re: Clearing direct format (Pyuno)

Post by imr_lor »

ARRRG unfortunatly after 3 days of work this feature is not longer needed.
All i had to do was to change this:

Code: Select all

def doHeading(self, titel, paraStyle, numberingLevel=1):
        self.cl_ID += 1
        doc = self.edit_doc
        cursor = doc.Text.createTextCursor()
        cursor.gotoEnd(False)
        cursor.ParaStyleName = paraStyle
        cursor.NumberingLevel = numberingLevel
        cursor.ListId = self.cl_ID
        if paraStyle == 'Heading 3':
            doc.Text.insertString(cursor, titel, 0)
        else:
            doc.Text.insertString(cursor, '\t' + titel, 0)
        doc.Text.insertControlCharacter(cursor, PARAGRAPH_BREAK, 0)
into this:

Code: Select all

def doHeading(self, titel, paraStyle, numberingLevel=1):
        self.cl_ID += 1
        doc = self.edit_doc
        cursor = doc.Text.createTextCursor()
        cursor.gotoEnd(False)
        cursor.ParaStyleName = paraStyle
        cursor.NumberingLevel = numberingLevel
        cursor.CharWeight = FW_BOLD
        cursor.ListId = self.cl_ID
        if paraStyle == 'Heading 3':
            doc.Text.insertString(cursor, titel, 0)
        else:
            doc.Text.insertString(cursor, '\t' + titel, 0)
        doc.Text.insertControlCharacter(cursor, PARAGRAPH_BREAK, 0)
:crazy: :crazy: :crazy:
But thanks everyone for the help with this.
OpenOffice 3.1 on LinuxMint 17.1
Post Reply