[Solved] Clearing direct format (Pyuno)
[Solved] Clearing direct format (Pyuno)
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
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
-
- Volunteer
- Posts: 1160
- Joined: Mon Oct 08, 2007 1:26 am
- Location: France, Paris area
Re: Clearing direct format
Use the dispatcher.
Bernard
OpenOffice.org 1.1.5 / Apache OpenOffice 4.1.1 / LibreOffice 5.0.5
MS-Windows 7 Home SP1
OpenOffice.org 1.1.5 / Apache OpenOffice 4.1.1 / LibreOffice 5.0.5
MS-Windows 7 Home SP1
Re: Clearing direct format
ok how do i do this?B Marcelly wrote:Use the dispatcher.
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
Re: Clearing direct format
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?
Try this on a copy file first - why bother with a macro?
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
Re: Clearing direct format
I have to do this because this is an automatically generated document which is generated based on an input from an JSON file.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?
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
-
- Volunteer
- Posts: 1160
- Joined: Mon Oct 08, 2007 1:26 am
- Location: France, Paris area
Re: Clearing direct format
If you don't know how to translate this in Python, stick to Basic.Thx very much for the tip(Dispatcher) now i only gonna have to figure out how to use this in Python
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
OpenOffice.org 1.1.5 / Apache OpenOffice 4.1.1 / LibreOffice 5.0.5
MS-Windows 7 Home SP1
Re: Clearing direct format
B Marcelly wrote:If you don't know how to translate this in Python, stick to Basic.Thx very much for the tip(Dispatcher) now i only gonna have to figure out how to use this in PythonCode: 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, ())
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
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Re: Clearing direct format
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?
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
Re: Clearing direct format (Pyuno)
Edit>Find&Replace...
[More Options]
[X] Search for style
[Find All]
Ctrl+M
[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
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Re: Clearing direct format (Pyuno)
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
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
Re: Clearing direct format (Pyuno)
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.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
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
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Re: Clearing direct format (Pyuno)
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:
gives the error:
ImportError: No module named 'com' (or 'com.sun.star.frame.DispatchHelper' is unknown)
but these imports are working totally fine:
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
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
Re: Clearing direct format (Pyuno)
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
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Re: Clearing direct format (Pyuno)
Mh ok.
Is the needed module implemented in UNO(PYUNO)?
Is the needed module implemented in UNO(PYUNO)?
OpenOffice 3.1 on LinuxMint 17.1
Re: Clearing direct format (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.imr_lor wrote:Mh ok.
Is the needed module implemented in UNO(PYUNO)?
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
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Re: Clearing direct format (Pyuno)
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
Re: Clearing direct format (Pyuno)
I modifed the findParaStyle so it only searches for the last
It seems the call getCurrentContoller produces an erro(exit code 139)
It seems the call getCurrentContoller produces an erro(exit code 139)
OpenOffice 3.1 on LinuxMint 17.1
Re: Clearing direct format (Pyuno)
ARRRG unfortunatly after 3 days of work this feature is not longer needed.
All i had to do was to change this:
into this:
But thanks everyone for the help with this.
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)
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)
But thanks everyone for the help with this.
OpenOffice 3.1 on LinuxMint 17.1