pip install Pygments
python
from pygments import highlight
from pygments.lexers import PythonLexer
from pygments.formatters import HtmlFormatter
code = '''
def hello_world():
print("Hello, World!")
hello_world()
'''
lexer = PythonLexer()
formatter = HtmlFormatter()
highlighted_code = highlight(code, lexer, formatter)
print(highlighted_code)
python
from pygments.styles import get_style_by_name
code = '''
def hello_world():
print("Hello, World!")
hello_world()
'''
lexer = PythonLexer()
style = get_style_by_name('colorful')
formatter = HtmlFormatter(style=style)
highlighted_code = highlight(code, lexer, formatter)
print(highlighted_code)