gpt4 book ai didi

python - 使用 pyperclip 将换行符添加到剪贴板?

转载 作者:行者123 更新时间:2023-12-01 05:11:59 37 4
gpt4 key购买 nike

我正在使用 pyperclip(允许你将内容放入剪贴板的 python 模块),虽然它适合放置单行,但如果我想让用户复制多行怎么办?放置“/n”只会将“/n”直接复制到字符串中。我还可以做些什么?这是 pyperclip 的 windows 函数:

def winSetClipboard(self, text):
text = str(text)
GMEM_DDESHARE = 0x2000
ctypes.windll.user32.OpenClipboard(0)
ctypes.windll.user32.EmptyClipboard()
try:
# works on Python 2 (bytes() only takes one argument)
hCd = ctypes.windll.kernel32.GlobalAlloc(GMEM_DDESHARE, len(bytes(text))+1) # @UndefinedVariable
except TypeError:
# works on Python 3 (bytes() requires an encoding)
hCd = ctypes.windll.kernel32.GlobalAlloc(GMEM_DDESHARE, len(bytes(text, 'ascii'))+1) # @UndefinedVariable
pchData = ctypes.windll.kernel32.GlobalLock(hCd) # @UndefinedVariable
try:
# works on Python 2 (bytes() only takes one argument)
ctypes.cdll.msvcrt.strcpy(ctypes.c_char_p(pchData), bytes(text))
except TypeError:
# works on Python 3 (bytes() requires an encoding)
ctypes.cdll.msvcrt.strcpy(ctypes.c_char_p(pchData), bytes(text, 'ascii'))
ctypes.windll.kernel32.GlobalUnlock(hCd) # @UndefinedVariable
ctypes.windll.user32.SetClipboardData(1, hCd)
ctypes.windll.user32.CloseClipboard()

最佳答案

正如评论中提到的,'\n' 是换行符的正确转义。此外,Windows 行结尾为 '\r\n'

关于python - 使用 pyperclip 将换行符添加到剪贴板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24037441/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com