$ pygmentize -O full -O style=native -o test.html test.py
你可以把它当图书馆用。在from pygments import highlight
from pygments.lexers import guess_lexer
from pygments.formatters import HtmlFormatter
code = '#!/usr/bin/python\nprint "Hello World!"'
lexer = guess_lexer(code) # or just pygments.lexers.PythonLexer()
formatter = HtmlFormatter(noclasses=True, nowrap=True, lineseparator="
\n")
print highlight(code, lexer, formatter)
输出:#!/usr/bin/python
"Hello World!"
(为清晰起见,添加了空格)
html格式:
#!/usr/bin/python
打印
“你好,世界!”