Skip to content

difflib-generated HTML file does not use monospaced font #131204

Closed
@tfpf

Description

@tfpf

Bug report

Bug description:

difflib.HtmlDiff.make_file writes an HTML file with a CSS rule specifying Courier as the font to use. On systems which don't have it installed, browsers may fall back to the system default font, which likely won't be monospaced.

import difflib
import webbrowser

hd = difflib.HtmlDiff()
html = hd.make_file(["a", "aab", "c" + "f" * 20, "eg", "ry"], ["aab", "f" * 19, "ry"])
with open("diff.html", "w") as writer:
    print(html, file=writer)
webbrowser.open("diff.html")

On my system (which does not have Courier), the default font is Signika, which is what Firefox used to render the text.

Image

  • Python 3.13.2 (main, Feb 5 2025, 08:05:21) [GCC 14.2.1 20250128]
  • glibc 2.41+r9+ga900dbaf70f0-1
  • Arch Linux (64-bit)

I think is is sufficient to do:

diff --git a/Lib/difflib.py b/Lib/difflib.py
index bc09aa128aa..11d286a4aa8 100644
--- a/Lib/difflib.py
+++ b/Lib/difflib.py
@@ -1633,7 +1633,7 @@ def _line_pair_iterator():
 
 _styles = """
         :root {color-scheme: light dark}
-        table.diff {font-family:Courier; border:medium;}
+        table.diff {font-family:monospace; border:medium;}
         .diff_header {background-color:#e0e0e0}
         td.diff_header {text-align:right}
         .diff_next {background-color:#c0c0c0}

and will follow up with a pull request soon.

CPython versions tested on:

3.13

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibPython modules in the Lib dirtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions