pythonGo
pythonGo

Reputation: 173

pyperclip copies only the last item on list cocatenation to clipboard

Totally a newbie to python. Please, i need pyperclip to copy the result of print to clipboard on this code.

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.')

This is the result i want to be copied.

10.jpg
20.jpg

But only the last line which is "20.jpg" copies to the clipboard.

20.jpg

Upvotes: 1

Views: 629

Answers (2)

pythonGo
pythonGo

Reputation: 173

I had a problem where pyperclip.copy only copied last string of my print function to the clipboard and i wanted it to copy the whole output. This is the solution i came up with some pointers from @pylang .

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.')

Now the whole user input() with the '.jpg' extension is generated and copied to the clipboard using pyperclip.

Upvotes: 1

pylang
pylang

Reputation: 44535

pyperclip.copy() accepts a single string. Using a modified example:

Given

import pyperclip


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

The results are some group of strings:

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

Code

Join the filenames into a single string, separated by newlines:

"\n".join(filenames)
# '10.jpg\n20.jpg'

Demo

Copy the last result with an underscore _:

pyperclip.copy(_)

Paste results, e.g. Ctrl + V:

10.jpg
20.jpg

Upvotes: 2

Related Questions