From e268eea9dfeca838989e864ac075eb689cf19277 Mon Sep 17 00:00:00 2001 From: Alexander Parrill Date: Tue, 30 Dec 2025 16:41:17 -0500 Subject: [PATCH] Show report headings + captions in Word bookmark list Remove the `_Ref` prefix from bookmark names in manually named headings and captions, making them visible in the MS Word bookmark list. Captions without a bookmark name will still generate a bookmark with the `_Ref` prefix. This change won't break existing `{{.ref}}`s (since the prefix is removed from there too) but will cause previously-hidden caption bookmarks to appear. Bookmarks can still be hidden by manually prefixing then name with `_Ref`. Fixes #707 --- ghostwriter/modules/reportwriter/richtext/docx.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/ghostwriter/modules/reportwriter/richtext/docx.py b/ghostwriter/modules/reportwriter/richtext/docx.py index 6b5e57db6..b4d479816 100644 --- a/ghostwriter/modules/reportwriter/richtext/docx.py +++ b/ghostwriter/modules/reportwriter/richtext/docx.py @@ -124,13 +124,13 @@ def _tag_h(self, el, **kwargs): tag = heading_paragraph.runs[0]._r start = docx.oxml.shared.OxmlElement("w:bookmarkStart") start.set(docx.oxml.ns.qn("w:id"), str(self.current_bookmark_id)) - start.set(docx.oxml.ns.qn("w:name"), "_Ref" + bookmark_name) + start.set(docx.oxml.ns.qn("w:name"), bookmark_name) tag.insert(0, start) tag = heading_paragraph.runs[-1]._r end = docx.oxml.shared.OxmlElement("w:bookmarkEnd") end.set(docx.oxml.ns.qn("w:id"), str(self.current_bookmark_id)) - end.set(docx.oxml.ns.qn("w:name"), "_Ref" + bookmark_name) + end.set(docx.oxml.ns.qn("w:name"), bookmark_name) tag.append(end) self.current_bookmark_id += 1 @@ -404,9 +404,7 @@ def make_caption(self, par, label: str, ref: str | None = None, styles = ["Capti except KeyError: continue - if ref: - ref = f"_Ref{ref}" - else: + if not ref: ref = f"_Ref{random.randint(10000000, 99999999)}" # Start a bookmark run with the figure label @@ -584,7 +582,7 @@ def make_cross_ref(self, par, ref: str): run = par.add_run() r = run._r instrText = OxmlElement("w:instrText") - instrText.text = ' REF "_Ref{}" \\h '.format(ref.replace("\\", "\\\\").replace('"', '\\"').replace(" ", "_")) + instrText.text = ' REF "{}" \\h '.format(ref.replace("\\", "\\\\").replace('"', '\\"').replace(" ", "_")) r.append(instrText) # An optional ``separate`` value to enforce a space between label and number