admin 管理员组

文章数量: 1086019

In a Libre Office Writer text document, i would like to reset all explicitely applied font size settings, both in text paragraphs as well as in tables. For paragraphs i can find such explicit settings by checking whether getPropertyState("CharHeight") returns DIRECT_VALUE. Sadly, when i also process tables in the document, the cell content does show a value of CharHeightwhich is different from the one in the Standard paragraph style, but when checking the property state it does return DEFAULT_VALUE which indicates that no explicit value has been set.

My code (executed as python macro) looks like this:

...
elements = doc.getText().createEnumeration()

while elements.hasMoreElements():
    element = elements.nextElement()
    
    if element.supportsService("com.sun.star.text.Paragraph"):
        
        # This works fine
        if element.getPropertyState("CharHeight") == DIRECT_VALUE:
            element.setPropertyToDefault("CharHeight")
        
    elif element.supportsService("com.sun.star.text.TextTable"):
        cell_names = element.getCellNames()

        for cell_name in cell_names:
            cell = element.getCellByName(cell_name)
            cell_cursor = cell.createTextCursor()
            
            # the following condition never results to be True for my document
            if cell_cursor.getPropertyState("CharHeight") == DIRECT_VALUE:
                cell_cursor.setPropertyToDefault("CharHeight")
...

Resetting the value to default has no effect, which is in line with the value not beeing directly set in the first place, just then i wonder, where does the actual value (15pt in my case) come from?

When i fetch all properties of the cell text curser in the original situation, and after resetting all direct formatting with CTRL+M, and compare the properties, then only 4 properties change. CharAutoStyleName gets cleared from some cryptic value to "0", CharHeight is reduced to the expected 12pt, CharHeightAsian also is reduced, and CharStyleNames is changed from an (empty) sequence to -void- .

Sadly i am able to reproduce this effect from the macro. Neither can i reset CharAutoStyleName programatically (i get a RuntimeException), nor CharStyleNames (which was an empty sequence anyway).

In case embedded picture does not work:

In a Libre Office Writer text document, i would like to reset all explicitely applied font size settings, both in text paragraphs as well as in tables. For paragraphs i can find such explicit settings by checking whether getPropertyState("CharHeight") returns DIRECT_VALUE. Sadly, when i also process tables in the document, the cell content does show a value of CharHeightwhich is different from the one in the Standard paragraph style, but when checking the property state it does return DEFAULT_VALUE which indicates that no explicit value has been set.

My code (executed as python macro) looks like this:

...
elements = doc.getText().createEnumeration()

while elements.hasMoreElements():
    element = elements.nextElement()
    
    if element.supportsService("com.sun.star.text.Paragraph"):
        
        # This works fine
        if element.getPropertyState("CharHeight") == DIRECT_VALUE:
            element.setPropertyToDefault("CharHeight")
        
    elif element.supportsService("com.sun.star.text.TextTable"):
        cell_names = element.getCellNames()

        for cell_name in cell_names:
            cell = element.getCellByName(cell_name)
            cell_cursor = cell.createTextCursor()
            
            # the following condition never results to be True for my document
            if cell_cursor.getPropertyState("CharHeight") == DIRECT_VALUE:
                cell_cursor.setPropertyToDefault("CharHeight")
...

Resetting the value to default has no effect, which is in line with the value not beeing directly set in the first place, just then i wonder, where does the actual value (15pt in my case) come from?

When i fetch all properties of the cell text curser in the original situation, and after resetting all direct formatting with CTRL+M, and compare the properties, then only 4 properties change. CharAutoStyleName gets cleared from some cryptic value to "0", CharHeight is reduced to the expected 12pt, CharHeightAsian also is reduced, and CharStyleNames is changed from an (empty) sequence to -void- .

Sadly i am able to reproduce this effect from the macro. Neither can i reset CharAutoStyleName programatically (i get a RuntimeException), nor CharStyleNames (which was an empty sequence anyway).

In case embedded picture does not work: https://ibb.co/MD81vmMN

Share Improve this question edited Apr 1 at 21:09 Janosch asked Mar 27 at 22:26 JanoschJanosch 1,2642 gold badges11 silver badges19 bronze badges 4
  • A few ideas: Character styles override paragraph styles — CharStyleName/CharStyleNameComplex/CharStyleNameAsian. For those, there are potentially multiple TextPortions within a single paragraph, so pay attention to where the cursor is. In MRI, try sorting the properties in alphabetical order to more easily see related properties. Also, there is such a thing as a table style, although to edit them requires unzipping the .odt file, and IIRC those are just initial defaults rather than actual settings. Anyway, unzipping the file to look at that place in content.xml could help find the issue. – Jim K Commented Mar 29 at 17:50
  • One more note: In the UI, there is a distinction between para/char styles and hard formatting. However, in the underlying file structure, even supposed hard formatting is actually done with styles, except they're automatically managed. – Jim K Commented Mar 29 at 18:08
  • Thanks for your hints @JimK ! I appended a paragraph to my question to mention an additional observation about how CTRL+M changes the properties, hinting that indeed CharAutoStyleName might be the culprit, just i could not find any way to programatically clear this property :-( – Janosch Commented Apr 1 at 21:06
  • Try the example of resetting CharAutoStyleName at forum.openoffice./en/forum/viewtopic.php?p=518969. Also, my answer here is partly related: stackoverflow/a/42409510/5100564. – Jim K Commented Apr 3 at 15:17
Add a comment  | 

1 Answer 1

Reset to default 0

I'm pretty sure that "auto" in CharAutoStyleName means what most people think of as direct formatting, as explained in my comment about automatically managed. So it means your text has some formatting that is not from a named style.

However, if the font and size don't seem to be set, and setPropertyToDefault for name and size isn't getting the job done, then maybe there's some orphaned formatting that didn't get cleaned out. As mentioned in the comments, unzipping would show whether this is the case.

I don't suppose setting CharAutoStyleName to 0 would fix the problem? The cryptic name is probably the generated id of the style definition in content.xml, which gets declared higher up in the file. Hopefully if you delete the name, then LibreOffice would recognize it and delete the definition, or not include the unused definition when saving the file.

The reliable and obvious solution would be to use the dispatcher to call the "Clear Direct Formatting" action. Use the macro recorder to figure out the name of the call. Here is an example of using the dispatcher.

oFrame = ThisComponent.CurrentController.Frame
oDispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
oDispatcher.executeDispatch(oFrame, ".uno:Reload", "", 0, Array())

本文标签: unoWhere do table cells get their CharHeight from in LibreOffice WriterStack Overflow