pyperclip只将列表组件上的最后一项复制到剪贴板



完全是python的新手。请,我需要pyperclip将打印结果复制到该代码的剪贴板上。

print ('These generates image variation titles and a .jpg file extension added to each number.')
vartop = []
while True:
print('Enter the variation ID ' + str(len(vartop) + 1) + ' (or Enter to Generate.):')
myVar = input()
if myVar == '':
break
vartop = vartop + [myVar]
print('Variations are: ')
for myVar in vartop:
print (myVar + '.jpg') #I want this result to be copied to the clipboard.
import pyperclip
pyperclip.copy(myVar + '.jpg') #This code copies only the last generated line to the clipboard.
print ('Variations Copied to clipboard.')

这是我想要复制的结果。

10.jpg
20.jpg

但只有最后一行"20.jpg"复制到剪贴板。

20.jpg

pyperclip.copy()接受单个字符串。使用修改后的示例:

给定

import pyperclip

filenames = [f"{x}.jpg" for x in range(10, 30, 10)]

结果是一些字符串组:

filenames
# ['10.jpg', '20.jpg']

代码

将文件名合并为一个字符串,用换行符分隔:

"n".join(filenames)
# '10.jpgn20.jpg'

演示

用下划线_:复制最后一个结果

pyperclip.copy(_)

粘贴结果,例如Ctrl + V:

10.jpg
20.jpg

我遇到了一个问题,pyperclip.copy只将打印函数的最后一个字符串复制到剪贴板,我希望它复制整个输出。这是我从@pillang那里得到的一些解决方案。

print ('These generates image variation titles and a .jpg file extension added to    each number.')
vartop = []
while True:
print('Enter the variation ID ' + str(len(vartop) + 1) + ' (or Enter to Generate.):')
myVar = input()
if myVar == '':
break
vartop = vartop + [myVar + '.jpg'] #I added the file extension to the list Concatenation
print('Variations are: ')
for myVar in vartop:
print(myVar)
import pyperclip
pyperclip.copy("n".join(vartop + [myVar])) #Using a list separator "n", i added the code i wanted in my clipboard to the pyperclip.copy without the file extension.
print ('Variation Titles Copied to clipboard.')

现在生成了具有'.jpg'扩展名的整个用户input(),并使用pyperclip将其复制到剪贴板。

最新更新