[Solved] Clearing direct format (Pyuno)

Creating a macro - Writing a Script - Using the API

[Solved] Clearing direct format (Pyuno)

Postby imr_lor » Tue Oct 13, 2015 10:17 am

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
imr_lor
 
Posts: 23
Joined: Fri Sep 04, 2015 10:28 am

Re: Clearing direct format

Postby B Marcelly » Tue Oct 13, 2015 1:32 pm

Use the dispatcher.
Bernard

OpenOffice.org 1.1.5 / Apache OpenOffice 4.1.1 / LibreOffice 5.0.5
MS-Windows 7 Home SP1
B Marcelly
Volunteer
 
Posts: 1160
Joined: Mon Oct 08, 2007 1:26 am
Location: France, Paris area

Re: Clearing direct format

Postby imr_lor » Tue Oct 13, 2015 1:52 pm

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
imr_lor
 
Posts: 23
Joined: Fri Sep 04, 2015 10:28 am

Re: Clearing direct format

Postby RoryOF » Tue Oct 13, 2015 1:58 pm

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.7 on Xubuntu 18.04.3 (mostly 64 bit version) and very infrequently on Win2K/XP
User avatar
RoryOF
Moderator
 
Posts: 29904
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Clearing direct format

Postby imr_lor » Tue Oct 13, 2015 2:25 pm

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
imr_lor
 
Posts: 23
Joined: Fri Sep 04, 2015 10:28 am

Re: Clearing direct format

Postby B Marcelly » Tue Oct 13, 2015 4:38 pm

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   Expand viewCollapse view
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
B Marcelly
Volunteer
 
Posts: 1160
Joined: Mon Oct 08, 2007 1:26 am
Location: France, Paris area

Re: Clearing direct format

Postby Villeroy » Tue Oct 13, 2015 11:36 pm

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   Expand viewCollapse view
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
dispatcher.executeDispatch(document, ".uno:ResetAttributes", "", 0, Array())

Code: Select all   Expand viewCollapse view
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, no OpenOffice, LibreOffice 6.x
User avatar
Villeroy
Volunteer
 
Posts: 27383
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Clearing direct format

Postby imr_lor » Wed Oct 14, 2015 4:26 pm

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   Expand viewCollapse view
def do_XXXXXXX(self):
        if self.XXXXXXXXXX('XXX'):
            self.doHeading('XXXXXXXXXXXXXXXX XXXXXXXXXX  XXXXX', "Heading 2")
            self.addParagraphBreak()

Code: Select all   Expand viewCollapse view
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
imr_lor
 
Posts: 23
Joined: Fri Sep 04, 2015 10:28 am

Re: Clearing direct format (Pyuno)

Postby Villeroy » Wed Oct 14, 2015 5:24 pm

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, no OpenOffice, LibreOffice 6.x
User avatar
Villeroy
Volunteer
 
Posts: 27383
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Clearing direct format (Pyuno)

Postby imr_lor » Thu Oct 15, 2015 8:21 am

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
imr_lor
 
Posts: 23
Joined: Fri Sep 04, 2015 10:28 am

Re: Clearing direct format (Pyuno)

Postby Villeroy » Thu Oct 15, 2015 8:43 am

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, no OpenOffice, LibreOffice 6.x
User avatar
Villeroy
Volunteer
 
Posts: 27383
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Clearing direct format (Pyuno)

Postby imr_lor » Thu Oct 15, 2015 10:02 am

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   Expand viewCollapse view
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]
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
imr_lor
 
Posts: 23
Joined: Fri Sep 04, 2015 10:28 am

Re: Clearing direct format (Pyuno)

Postby Villeroy » Thu Oct 15, 2015 10:17 am

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, no OpenOffice, LibreOffice 6.x
User avatar
Villeroy
Volunteer
 
Posts: 27383
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Clearing direct format (Pyuno)

Postby imr_lor » Thu Oct 15, 2015 10:21 am

Mh ok.
Is the needed module implemented in UNO(PYUNO)?
OpenOffice 3.1 on LinuxMint 17.1
imr_lor
 
Posts: 23
Joined: Fri Sep 04, 2015 10:28 am

Re: Clearing direct format (Pyuno)

Postby Villeroy » Thu Oct 15, 2015 10:46 am

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   Expand viewCollapse view
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, no OpenOffice, LibreOffice 6.x
User avatar
Villeroy
Volunteer
 
Posts: 27383
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Clearing direct format (Pyuno)

Postby imr_lor » Thu Oct 15, 2015 10:52 am

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)

Postby imr_lor » Thu Oct 15, 2015 1:14 pm

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)

Postby imr_lor » Thu Oct 15, 2015 3:43 pm

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   Expand viewCollapse view
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   Expand viewCollapse view
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
imr_lor
 
Posts: 23
Joined: Fri Sep 04, 2015 10:28 am


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 5 guests