# Ensure Writer doc
if not doc.supportsService("com.sun.star.text.TextDocument"):
try:
parent_win = doc.CurrentController.Frame.ContainerWindow
mb = parent_win.getToolkit().createMessageBox(
parent_win, MESSAGEBOX, MBButtons.BUTTONS_OK,
"Macro Report", "This macro can only run on a Writer document."
)
mb.execute()
except:
pass
return
# Build regex with hex escapes (ICU): \x{HHHH}
# Note: double backslash in Python to send a single backslash to LO
alts = [f"\\x{{{ord(ch):04X}}}" for ch in fake_chars.keys()]
pattern = "(?:" + "|".join(alts) + ")"
# Count matches per char BEFORE replacement
counts = {ch: 0 for ch in fake_chars.keys()}
found = doc.findAll(sd)
for i in range(found.getCount()):
rng = found.getByIndex(i)
s = rng.getString() # the exact matched char
if s in counts:
counts[s] += 1
total = sum(counts.values())
if total == 0:
return # nothing to do
# Replace all in one go
sd.setReplaceString(zwnj)
doc.replaceAll(sd)
# Build English report (only those replaced)
lines = []
for ch, name in fake_chars.items():
c = counts[ch]
if c > 0:
lines.append(f"{name} (U+{ord(ch):04X}): {c} replaced")
report = f"Total {total} fake hyphens found and replaced.\n" + "\n".join(lines)