a python script for the table of contents

Previous topic - Next topic

utnik

hi ale

something was broken on another level.
after a restart of the computer your script works as it should.
i'm sorry!

thank you for your work!

utnik

sersha

First, let me thank you for the very much needed element in Scribus namely TOC creation! I am producing a book over 300 pages in Hindi (and I do not even speak the language) and it went rather smoothly except for footnotes, most of which I set by hand.
Now I came to the TOC, suffered a bit with the built-in function, then, rather frustrated, came upon your script! I downloaded it per your instructions, then I created styles h1->h5, then toc1-toc5, edited the script to include the headers, marked the headings accordingly in the text, then run the script after the selection of a placeholder linked text frames... but no luck. Out came the following:

Traceback (most recent call last):
  File "<string>", line 11, in <module>
  File "<string>", line 163, in <module>
  File "<string>", line 139, in main
  File "<string>", line 82, in get_frame_headings_by_style
scribus.WrongFrameTypeError: Cannot get text of non-text frame.

------------------
Why could that be, and what could have been done to make it run?
Thank you again very much for the valuable work! Best regards, Sergey

a.l.e

i did not test the script with the toc in linked frames.
i did it now and it does work.
that's not the issue

the error says that the script is trying to do "selectFrameText()" on a frame that is not a text frame.

the script is not thoroughly tested (but thanks to utnik at least tested) and there might be some edge cases that are not correctly handled.

if you share the .sla file with me (us) i can try to find which frame is the culprit and why...
or you can try to debug it (and share the results so that i can improve the script) by printing the items while the script goes through and checking what is special in the frame that fails.

sersha

Thank you so much for your prompt reply!
I enclose the exported working file minus pictures, together with fonts used. In the beginning of the document, I left some linked text frames for TOC. I also enclose my amendments to the script, just in case I spoiled anything inadvertently. Best regards, Sergeytable-of-contents.py.zipEkachakra no pictures.slafonts.zip   

a.l.e

#19
no idea why i got to the idea that item[1] == 5 is for linked text frames.

anyway, simply using item[1] == 4 only works well:

https://github.com/aoloe/scribus-script-repository/commit/03c96173d8142c05437c11f09bf2ea759751de75

the way i debugged this, was by adding

print(scribus.getSelectedObject())
at the beginning of get_frame_headings_by_style() ... and, after starting from a terminal, it told me that the last item it saw, was a line...

et voilà!

you can make the same change to your version of the script or download my script again and adapt it.

sersha

Great, thanks! Already it worked wonders and saved my day! Some small questions remain:
  • In the "Sections" I opted to have Dedication and Gratitude to have Roman numbers, and it is reflected correctly on the real pages. But somehow it did not appear in the TOC. "Reviews" are starting with normal numeration, and the rest of TOC is correct.
  • Then "The title page" and the chapter titles are in the style "toc1" which is bold. But all other subtitles should be in the regular font. Somehow, up to Chapter-3 it keeps the bold attribute, but the rest of TOC is just fine.
I am positively amazed how nicely the script positioned all the 5 levels of TOC (see the ch 4 as example). And thank you for hinting how to print out the found header frames, I thought to ask but hesitated. Anyway, I am totally new to Python... Appreciate you work a lot! Sergey.

sersha

I do not know how, but I eliminated the "smearing" of the bold. It was a remnant of a previous style for headers left between lines.
So, few, maybe obvious rules.
One header, one line of a particular style. Otherwise and quite logically, it is considered to be yet another entry.
Eliminate unnecessary styles left over from previous experiments. They can play a joke on you!
I still cannot figure out the cause of the page break after the Ch 5...
But the rest seems to be coming out just fine! Enclose the latest results so far.
Regards, Sergeyno pictures p1-5.pdf

sersha

At last, a success! A near perfect TOC. The page break after the chapter 5 disappeared when I unlinked frames between second and third headers after the chapter title, respective pages 237 and 238. I do not understand, why it worked this way, but it did.
Now, the only thing left for the perfection would be the transfer of appropriate Roman numeration... I enclose the last sla and TOC.
Thank you again, a.l.e!

a.l.e

#23
voilà, i've pushed a much shorter version of the script that uses the page numbers as defined in the document sections (not in the sections variable).

https://github.com/aoloe/scribus-script-repository/tree/master/table-of-contents

the script checks if the new function is available and then uses it.

if it's not available, it will show page numbers from 1 to n.

(you will need a scribus newer than the 24. of january 2023 to get the page numbers as defined in the sections)

if you want to use the section structure with an older version of scribus, you can use this "older" version of the script (the last one with the "sections" variable:

https://github.com/aoloe/scribus-script-repository/tree/03c96173d8142c05437c11f09bf2ea759751de75/table-of-contents