Qu

docx.add_toc

Builtin index · File I/O

docx.add_toc(doc, [levels=3], [at=], [title=])

Input: doc, levels, at, title · Output: no output

What it is

Inserts a table-of-contents field for heading levels 1 to levels, before body paragraph at (or at the end), optionally under a title paragraph. Qu does not lay out pages, so it does not invent entries: the field is marked dirty and the document asks Word to update fields on open. LibreOffice does not rebuild a TOC when it opens a .docx (so neither does docx.to_pdf): until someone updates it (Tools > Update > Update All), it shows a line saying so. *Added in v0.4.5*

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
docx.add_toc(doc, [levels=3], [at=], [title=])
doc —not documented yet
levels optional (default 3)Inserts a table-of-contents field for heading levels 1 to levels, before body paragraph at (or at the end), optionally under a title paragraph.
at optionalInserts a table-of-contents field for heading levels 1 to levels, before body paragraph at (or at the end), optionally under a title paragraph.
title optionalInserts a table-of-contents field for heading levels 1 to levels, before body paragraph at (or at the end), optionally under a title paragraph.
not documented yetQu does not lay out pages, so it does not invent entries: the field is marked dirty and the document asks Word to update fields on open. LibreOffice does not rebuild a TOC when it opens a .docx (so neither does docx.to_pdf): until someone updates it (Tools > Update > Update All), it shows a line saying so. *Added in v0.4.5*.

Examples

From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.

import docx
import pptx
import xlsx

doc = docx.open("paper.docx")
docx.replace_text(doc, "2025", "2026")
docx.add_heading(doc, "Results", 1)
docx.add_paragraph(doc, "The measured impedance was 42.3 ohm.", bold = true)
docx.add_table(doc, table(f = [1000, 2000], Z = [42.3, 40.1]))
docx.add_image(doc, "nyquist.png", width = 12 cm)
docx.add_link(doc, 1, "https://example.org/eis-primer", on = "impedance")
docx.add_comment(doc, 2, "Check this value", on = "42.3", author = "Ahmed")
docx.set_footer(doc, "Page #page of #pages", align = "center")
docx.add_toc(doc, at = 0, title = "Contents")      # Word builds it on open

docx.save_as(doc, "paper_2026.docx")
docx.to_pdf(doc, "paper_2026.pdf")          # needs LibreOffice

docx.to_latex(doc, "latex/paper.tex")       # images go to latex/figures/


p = pptx.open("lecture.pptx")
s = pptx.add_slide(p, title = "Impedance Spectroscopy", body = ["Nyquist", "Bode"])
pptx.add_image(p, s, "eis.png", x = 120 mm, y = 40 mm, w = 100 mm)
pptx.save_as(p, "lecture_new.pptx")

wb = xlsx.open("results.xlsx")
xlsx.set_cell(wb, "Results", "A1", "Frequency")
xlsx.fill_formula(wb, "Results", "C2:C100", "=A2*B2")
xlsx.freeze_panes(wb, "Results", "A2")
xlsx.save_as(wb, "results_new.xlsx")

not run by the build — this one needs something the repository cannot supply · from file-io