Mejora fichero temporal

Evitamos sobreescribir capturas de código previas
Capturas se dejan en /tmp
This commit is contained in:
luisgulo 2023-10-10 12:33:45 +02:00
parent e71b412981
commit b76278fafd

View File

@ -1,5 +1,6 @@
#!/usr/bin/python
import os
import tempfile
import pyperclip
from pygments import highlight
from pygments.lexers import get_lexer_by_name
@ -12,8 +13,12 @@ lenguaje = get_lexer_by_name("python", stripall=True)
# Recoger el codigo del Portapapeles
codigo = pyperclip.paste()
# 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="/tmp/codigo.png")
img = highlight(codigo, lenguaje, ImageFormatter(style='native'), outfile=fichero.name)
# Abrir la imagen generada
os.system('open /tmp/codigo.png')
os.system('open ' + fichero.name)