Python 3 Pygments Example to Syntax Highlight Source Code Using Themes in Browser on Command Line Full Project For Beginners

 

 

pip install pygments

 

 

python code usage

 

 

app.py

 

 

from pygments import highlight
from pygments.lexers import PythonLexer
from pygments.formatters import HtmlFormatter

code = 'print "Hello World"'
print(highlight(code, PythonLexer(), HtmlFormatter()))

 

 

python app.py

 

 

 

Command Line Usage

 

 

pygmentize -f html -O full -o test.html test.py

 

 

where the test.py is the python code that is to be highlighted it could be anything source code of any programming language

 

 

when you run this an html file will be created in the same directory and if you open that you will see the highlighted source code

 

 

 

 

 

Leave a Reply