Skip to content

Commit

Permalink
Merge pull request #465 from domwhewell-sage/docx-heading-bookmarks
Browse files Browse the repository at this point in the history
Add bookmarks to headings that have the `id` attribute
  • Loading branch information
chrismaddalena authored Jul 24, 2024
2 parents 0a124cf + 6c81127 commit d5a5422
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion ghostwriter/modules/reportwriter/richtext/docx.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,18 @@ def tag_br(self, el, *, par=None, **kwargs):
def _tag_h(self, el, **kwargs):
heading_num = int(el.name[1:])
self.text_tracking.new_block()
self.doc.add_heading(el.text, heading_num)
heading_paragraph = self.doc.add_heading(el.text, heading_num)
if "id" in el.attrs:
run = heading_paragraph.runs[0]
tag = run._r
start = docx.oxml.shared.OxmlElement('w:bookmarkStart')
start.set(docx.oxml.ns.qn('w:id'), '0')
start.set(docx.oxml.ns.qn('w:name'), el.attrs["id"])
tag.append(start)
end = docx.oxml.shared.OxmlElement('w:bookmarkEnd')
end.set(docx.oxml.ns.qn('w:id'), '0')
end.set(docx.oxml.ns.qn('w:name'), el.attrs["id"])
tag.append(end)

tag_h1 = _tag_h
tag_h2 = _tag_h
Expand Down

0 comments on commit d5a5422

Please sign in to comment.