jdum / odfdo

python library for OpenDocument format (ODF)
Apache License 2.0
55 stars 12 forks source link

How to update a TOCs (or any updateable field) #35

Closed mlwesoly closed 7 months ago

mlwesoly commented 7 months ago

Hello,

following setting: I'm loading an existing odt (that already has a TOC) and add extra text and also headers using odfdo. Then I like to update the TOC also with odfdo, but do not manage to do that. I try to get the element with something like: doc.body.get_toc(0) and then call .fill() I also tried to look up and down, but couldn't make it work. Also going through the doc and the examples. I just started to work with odfdo and odt in this way, so there is some understanding missing. How can I updated for example the existing TOC?

jdum commented 7 months ago

Hi, What you did seems correct. The main trap with odfdo is to know if an element is currently attached to the document or a detached copy. However, here all should work nicely ? I try a quick test in the following lines (using the result file from the recipe 'create_a_basic_text_document_with_a_table_of_content' as input document).

from odfdo import *

doc = Document('recipes/recipes_output/add_toc/document.odt')
toc = doc.body.get_toc()
header = Header(1,'HHHH')
doc.body.append(header)
toc.fill()
print(str(toc).split()[-6:])  # to print only the end of TOC
doc.save()

and I get the expected result (an added header in the TOC): ['links', '1.18.1.', 'Official', 'sites', '2.', 'HHHH']

and the saved document has it too.

So I don't know what breaks in your case.

mlwesoly commented 7 months ago

Thank you very much for the fast and helpful answer. I tried with a new odt and everything was working like a charm! So I didn't thought it before, but the odt I tried before is not really clean build. I think parts came from an old *.swx file, so I think this makes it not work. Will make a new clean template and check if it then works. Thank you again!

mlwesoly commented 7 months ago

After some playing with this topic I also stumbled over issues even when I use a simple new odt file. I'm still not 100% sure when it arises, but what i did was:

just as a first clue. I will check more precise when it comes, but maybe you already have an idea? thank you!

EDIT: udpate: I looked into the toc.py and {header.text} not always gives the title, but sometimes an empty string. but when i print {header} the whole name is printed.

EDIT2: Also I stumbled upon the issue, if the TOC has no title, he gives an error something like this would solve it, but i didn't check if this make somewhere else problems image

jdum commented 7 months ago

Hi, Thanks for the report. I can confirm they are bugs: str(header) and header.text are not ok, and the title also is broken. I expect some fixes soon...

jdum commented 7 months ago

I've just committed a version 3.5.1. It should fix most issues (empty TOC headers, problem with empty title). However I think the disappearing headers in the TOC list are related to "headers" being actually bookmarks (aka links), which is not currently supported by default. It is probably feasible to force TOC content to be bookmarks, but I did not test it.

mlwesoly commented 7 months ago

Amazing, thank you for the fast fix and the great lib anyway. As soon as i have time to work with it again, I will try out more.