⚡
usefultools.dev
</>

Python-Syntax-Highlighter: Code zu HTML

NEU

Python-Syntax-Highlighter, der Code in farbiges HTML mit Inline-Styles oder CSS-Klassen umwandelt, mit hellen und dunklen Themes und optionalen Zeilennummern. Kostenlos, im Browser.

Läuft zu 100 % in deinem Browser — deine Eingaben werden nie hochgeladen oder auf unseren Servern gespeichert.

Vorschau

1from dataclasses import dataclass
2
3
4@dataclass
5class Point:
6    x: float = 0.0
7    y: float = 0.0
8
9    def distance(self, other: "Point") -> float:
10        """Euclidean distance to another point."""
11        return ((self.x - other.x) ** 2 + (self.y - other.y) ** 2) ** 0.5
12
13
14if __name__ == "__main__":
15    p = Point(3, 4)
16    print(f"distance = {p.distance(Point()):.1f}")  # 5.0

Eigenständig: Füge es in einen Blog-Editor, eine E-Mail oder ein CMS ein, das <style>-Tags entfernt.

<pre style="margin:0;padding:12px 16px;background:#f6f8fa;color:#1f2328;font-family:ui-monospace, SFMono-Regular, Menlo, Consolas, 'Liberation Mono', monospace;font-size:14px;line-height:1.5;tab-size:4;overflow-x:auto;border-radius:6px"><code><span style="display:inline-block;min-width:2ch;margin-right:16px;text-align:right;color:#8c959f;user-select:none">1</span><span style="color:#cf222e">from</span> dataclasses <span style="color:#cf222e">import</span> dataclass
<span style="display:inline-block;min-width:2ch;margin-right:16px;text-align:right;color:#8c959f;user-select:none">2</span>
<span style="display:inline-block;min-width:2ch;margin-right:16px;text-align:right;color:#8c959f;user-select:none">3</span>
<span style="display:inline-block;min-width:2ch;margin-right:16px;text-align:right;color:#8c959f;user-select:none">4</span><span style="color:#8250df">@dataclass</span>
<span style="display:inline-block;min-width:2ch;margin-right:16px;text-align:right;color:#8c959f;user-select:none">5</span><span style="color:#cf222e">class</span> <span style="color:#953800">Point</span>:
<span style="display:inline-block;min-width:2ch;margin-right:16px;text-align:right;color:#8c959f;user-select:none">6</span>    x: <span style="color:#8250df">float</span> <span style="color:#cf222e">=</span> <span style="color:#0550ae">0.0</span>
<span style="display:inline-block;min-width:2ch;margin-right:16px;text-align:right;color:#8c959f;user-select:none">7</span>    y: <span style="color:#8250df">float</span> <span style="color:#cf222e">=</span> <span style="color:#0550ae">0.0</span>
<span style="display:inline-block;min-width:2ch;margin-right:16px;text-align:right;color:#8c959f;user-select:none">8</span>
<span style="display:inline-block;min-width:2ch;margin-right:16px;text-align:right;color:#8c959f;user-select:none">9</span>    <span style="color:#cf222e">def</span> <span style="color:#6639ba">distance</span>(<span style="color:#953800">self</span>, other: <span style="color:#0a3069">&quot;Point&quot;</span>) <span style="color:#cf222e">-&gt;</span> <span style="color:#8250df">float</span>:
<span style="display:inline-block;min-width:2ch;margin-right:16px;text-align:right;color:#8c959f;user-select:none">10</span>        <span style="color:#0a3069">&quot;&quot;&quot;Euclidean distance to another point.&quot;&quot;&quot;</span>
<span style="display:inline-block;min-width:2ch;margin-right:16px;text-align:right;color:#8c959f;user-select:none">11</span>        <span style="color:#cf222e">return</span> ((<span style="color:#953800">self</span>.x <span style="color:#cf222e">-</span> other.x) <span style="color:#cf222e">**</span> <span style="color:#0550ae">2</span> <span style="color:#cf222e">+</span> (<span style="color:#953800">self</span>.y <span style="color:#cf222e">-</span> other.y) <span style="color:#cf222e">**</span> <span style="color:#0550ae">2</span>) <span style="color:#cf222e">**</span> <span style="color:#0550ae">0.5</span>
<span style="display:inline-block;min-width:2ch;margin-right:16px;text-align:right;color:#8c959f;user-select:none">12</span>
<span style="display:inline-block;min-width:2ch;margin-right:16px;text-align:right;color:#8c959f;user-select:none">13</span>
<span style="display:inline-block;min-width:2ch;margin-right:16px;text-align:right;color:#8c959f;user-select:none">14</span><span style="color:#cf222e">if</span> <span style="color:#8250df">__name__</span> <span style="color:#cf222e">==</span> <span style="color:#0a3069">&quot;__main__&quot;</span>:
<span style="display:inline-block;min-width:2ch;margin-right:16px;text-align:right;color:#8c959f;user-select:none">15</span>    p <span style="color:#cf222e">=</span> Point(<span style="color:#0550ae">3</span>, <span style="color:#0550ae">4</span>)
<span style="display:inline-block;min-width:2ch;margin-right:16px;text-align:right;color:#8c959f;user-select:none">16</span>    <span style="color:#8250df">print</span>(<span style="color:#0a3069">f&quot;distance = {p.distance(Point()):.1f}&quot;</span>)  <span style="color:#6e7781;font-style:italic"># 5.0</span></code></pre>
⚡

Schnelle Beispiele

Typische Eingaben und die Ergebnisse, die dieses Tool liefert.

Schlüsselwort
def
<span class="py-kw">def</span>
Built-in
len(x)
<span class="py-bi">len</span>(x)
Konstante
None
<span class="py-const">None</span>
Kommentar
# note
<span class="py-com"># note</span>
📖

Überblick

Dieser Python-Syntax-Highlighter wandelt einen Ausschnitt in farbiges HTML um, das du in einen Blogbeitrag, eine Dokumentationsseite, einen Newsletter oder eine Folie einfügen kannst. Er nutzt einen echten Python-3.12-Lexer, sodass f-Strings mit verschachtelten Anführungszeichen, Raw- und Byte-Strings, dreifach begrenzte Docstrings, Dekoratoren, Zahlen wie 1_000.5e3j, die weichen Schlüsselwörter match/case und Built-ins wie len oder print korrekt eingefärbt werden, statt per regulärem Ausdruck erraten zu werden.

Wähle ein helles Theme (GitHub Light, Solarized Light) oder ein dunkles (One Dark, Monokai) und schalte Zeilennummern ein oder aus. Kopiere das Ergebnis in zwei Formen: eigenständiges HTML mit Inline-Styles, das auch E-Mail-Clients und Editoren übersteht, die Stylesheets entfernen, oder kompaktes HTML mit py-*-Klassen plus CSS-Block für Seiten, auf denen du das Stylesheet kontrollierst.

Zeilennummern sind als nicht auswählbar markiert, sodass Leser den Code ohne sie kopieren können. Das gesamte Hervorheben läuft in deinem Browser.

Warum dieses Tool?

  • Präzise Farben dank eines echten Python-Tokenizers
  • Funktioniert in E-Mail- und CMS-Editoren über Inline-Styles
  • Zwei helle und zwei dunkle Themes
  • Zeilennummern, die nicht mitkopiert werden
  • Auf der Zielseite wird kein JavaScript benötigt
⭐

Funktionen

🎨

Vier Themes

GitHub Light, Solarized Light, One Dark und Monokai, live in der Vorschau.

🐍

Python-Lexer

Verarbeitet f-Strings, Raw-/Byte-Strings, Docstrings, Dekoratoren, weiche Schlüsselwörter und def-/class-Namen.

✉️

HTML mit Inline-Styles

Jede Farbe steht direkt im Markup und wird daher überall dargestellt, auch in E-Mails.

🧾

Klassen + CSS

Schlankes Markup mit py-kw, py-str und Co. plus das passende Stylesheet.

🔢

Zeilennummern

Optional, rechtsbündig und von der Textauswahl ausgenommen.

🔒

Maskierte Ausgabe

Zeichen wie < und & in deinem Code werden HTML-maskiert, der Ausschnitt kann die Seite also nicht zerstören.

🛠️

So funktioniert’s

1

Code einfügen

Füge den Python-Ausschnitt in das Feld „Python-Code“ ein.

2

Theme wählen

Wähle das Theme, die Tab-Breite und ob Zeilennummern angezeigt werden sollen.

3

Format wählen

Wähle unter der Vorschau „Inline-Styles“ oder „Klassen + CSS“.

4

Kopieren und einfügen

Klicke auf „HTML kopieren“ (und „CSS kopieren“) und füge es in deine Seite ein.

🤔

Häufig gestellte Fragen