jdum / odfdo

python library for OpenDocument format (ODF)
Apache License 2.0
58 stars 11 forks source link

How to suppress number of headers and limit TOC depth? #29

Closed michael-newsrx closed 1 year ago

michael-newsrx commented 1 year ago

I've tried the suppress_numbering parameter to Header() but it does not seem to have any impact when I load the document up in LibreOffice. Trying to prevent numbering for depth 2 and lower and for depth 2 and lower to be excluded from TOC.

Suggestions?

jdum commented 1 year ago

Hi, short answer: use toc.outline_level = 1 (see example below).

Long answer: Seems a bug or bad code, I dont know what is this attribute "suppress_numbering", maybe from standard version 1.0 or non-standard idea?, but certainly not in v1.2 of standard. And also missing attribute "is-list-header" that would be cool to have.

Example:

from odfdo import Document, Header, Paragraph, TOC

doc = Document("text")
body = doc.body
toc = TOC()
toc.title = "title"
body.append(toc)

body.append(Header(1, "level 1"))
body.append(Header(2, "level 2"))
body.append(Header(3, "level 3"))
body.append(Paragraph("content aaa"))
body.append(Header(3, "level 3"))
body.append(Paragraph("content aab"))
body.append(Header(2, "level 2 - B"))
body.append(Header(3, "level 3 - B1"))
body.append(Paragraph("content aba"))
body.append(Header(3, "level 3 - B2"))
body.append(Paragraph("content abb"))

toc.outline_level = 2
toc.fill()
doc.save(target="test.odt")

result like:

Table of Contents
1. level 1
1.1. level 2
1.2. level 2 - B
michael-newsrx commented 1 year ago

Thanks!

This is a big help.