将带有pygments突出显示的代码发送到Windows剪贴板

时间:2017-10-18 13:01:47

标签: python windows clipboard pygments

我正在寻找可能提供与以下相关的替代方法/修改的答案:

  • 我将数据发送到剪贴板的方法
  • Lexer的选择

这可能会解决以下问题......

我的目标是能够通过某些进程将原始python发送到我的剪贴板,以便我可以将其直接粘贴到Microsoft应用程序(例如Outlook)中作为格式化文本,如下所示

enter image description here

我只能实现以下原始pygments输出。 RtfFormatter显示HtmlFormattercopy

同样如此

enter image description here

复制到剪贴板方法

我使用帖子https://stackoverflow.com/a/27291478/4013571中的import ctypes def copy(text): """Copies a string, ``text``, to Windows Clipboard (https://stackoverflow.com/a/27291478/4013571)""" GMEM_DDESHARE = 0x2000 CF_UNICODETEXT = 13 d = ctypes.windll # cdll expects 4 more bytes in user32.OpenClipboard(None) try: # Python 2 if not isinstance(text, unicode): text = text.decode('mbcs') except NameError: if not isinstance(text, str): text = text.decode('mbcs') d.user32.OpenClipboard(0) d.user32.EmptyClipboard() hCd = d.kernel32.GlobalAlloc(GMEM_DDESHARE, len(text.encode('utf-16-le')) + 2) pchData = d.kernel32.GlobalLock(hCd) ctypes.cdll.msvcrt.wcscpy(ctypes.c_wchar_p(pchData), text) d.kernel32.GlobalUnlock(hCd) d.user32.SetClipboardData(CF_UNICODETEXT, hCd) d.user32.CloseClipboard() func  如

from pygments import highlight
from pygments.lexers import PythonLexer
from pygments.formatters import RtfFormatter  # Same occurs using HtmlFormatter

raw_python = '''
def merge_two_dicts(x, y):
    """Given two dicts, merge them into a new dict as a shallow copy."""
    z = x.copy()
    z.update(y)
    return z
'''

highlighted = highlight(raw_python, PythonLexer(), RtfFormatter())
copy(highlighted)

我也试过pyperclip.copy但没有成功

语法突出显示方法

gs -q -dBATCH -dSAFER -dNOPAUSE \
-sDEVICE=tiff32nc \
-sDefaultRGBProfile=sRGB2014.icc \
-dOverrideICC \
-sOutputICCProfile=PSOcoated_v3.icc \
-sProcessColorModel=DeviceCMYK \
-sColorConversionStrategy=CMYK \
-sOutputFile=rgb.pdf \
 cmyk.tiff

0 个答案:

没有答案