Actually you don’t need to create a new style. You could just click right mouse button for a “Heading” with unwanted numbering, next in the popup menu choose «Paragraph… → Numbering style → None».
Sometimes it is grayed out for some reason, though… Anyway, I wrote a function in PyUNO which was always working for me — even if the submenu for some reason grayed out. I’ll leave it here if anybody would want it:
HEADING1 = "Heading 1"
HEADING2 = "Heading 2"
def removeNumberingSomeHeading1s(text):
"""Removes numbering from the first Heading1 (intro), then skips two heading1s (chapters), and
removes from the rest Heading1s and Heading2s"""
enumeration = text.createEnumeration()
heading1s = 0
while enumeration.hasMoreElements():
par = enumeration.nextElement()
if ( par.supportsService("com.sun.star.text.Paragraph") and
par.ParaStyleName != None):
if par.ParaStyleName == HEADING1:
heading1s = heading1s + 1
if ((heading1s > 3 or heading1s == 1) and
par.ParaStyleName == HEADING1 or
par.ParaStyleName == HEADING2):
par.setPropertyValue("NumberingStyleName", "NONE")
Though I am using it in a script with another things, I guess you can use it as a macro in the LO itself (I didn’t try). Also I guess you’d need to update TOC after the work is done.
I’ll also leave here a full working test code that I used. But note that for the test code to work you need to start libreoffice as a server before, like soffice "--accept=socket,host=localhost,port=8100;urp;" --headless --invisible
import uno
# that's like a consts, but they isn't since consts not allowed in python 😝
HEADING1 = "Heading 1"
HEADING2 = "Heading 2"
def removeNumberingSomeHeading1s(text):
"""Removes numbering from the first Heading1 (intro), then skips two heading1s (chapters), and
removes from the rest Heading1s and Heading2s"""
enumeration = text.createEnumeration()
heading1s = 0
while enumeration.hasMoreElements():
par = enumeration.nextElement()
if ( par.supportsService("com.sun.star.text.Paragraph") and
par.ParaStyleName != None):
if par.ParaStyleName == HEADING1:
heading1s = heading1s + 1
if ((heading1s > 3 or heading1s == 1) and
par.ParaStyleName == HEADING1 or
par.ParaStyleName == HEADING2):
par.setPropertyValue("NumberingStyleName", "NONE")
localContext = uno.getComponentContext()
resolver = localContext.ServiceManager.createInstanceWithContext(
"com.sun.star.bridge.UnoUrlResolver", localContext )
smgr = resolver.resolve( "uno:socket,host=localhost,port=8100;urp;StarOffice.ServiceManager" )
remoteContext = smgr.getPropertyValue( "DefaultContext" )
desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",remoteContext)
file = desktop.loadComponentFromURL("file:///tmp/test.odt" ,"_blank", 0, ())
removeNumberingSomeHeading1s(file.Text)
file.storeAsURL("file:///tmp/test3.odt",())
file.dispose()