gnuCodeSnap/gnuSnapCode.py

24 lines
677 B
Python
Raw Normal View History

2023-10-10 00:06:31 +02:00
#!/usr/bin/python
import os
import tempfile
import pyperclip
2023-10-10 00:06:31 +02:00
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import ImageFormatter
from pygments.formatters.img import PilNotAvailable
# Definir lenguaje (sintaxis)
2023-10-10 00:06:31 +02:00
lenguaje = get_lexer_by_name("python", stripall=True)
# Recoger el codigo del Portapapeles
codigo = pyperclip.paste()
2023-10-10 00:06:31 +02:00
# fichero temporal para nombre unico y no sobreescribir
fichero = tempfile.NamedTemporaryFile()
fichero.name += '.png'
# Generar la imagen
img = highlight(codigo, lenguaje, ImageFormatter(style='native'), outfile=fichero.name)
2023-10-10 00:06:31 +02:00
# Abrir la imagen generada
os.system('open ' + fichero.name)