2

Ok so I'm working on Chapter 6 on Automate the Boring stuff and I'm having trouble understanding how to get it to run the project. It runs but all that comes up is the "Press any key to continue...". It's like I can't input and string for it to work... or at least I think that's how it's supposed to go. I'm not the best with pyperclip or getting things to run yet.

Can anyone help me understand how I can get this to work, so I can have some output? I'm not sure how to use the clipboard in the cmd line either, any ideas?

#! python3
# bulletPointAdder.py - Adds Wikipedia bullet points to the start
# of each line of text on the clipboard.

import pyperclip
text = pyperclip.paste()

# Separate lines and add stars.
lines = text.split('\n')
for i in range(len(lines)):    # loop through all indexes for "lines" list
    lines[i] = '* ' + lines[i] # add star to each string in "lines" list
text = '\n'.join(lines)
pyperclip.copy(text)

This is the bin file I'm using:

 @py C:\Users\david\MyPythonScripts\AddingBullets.py %*
@pause
MikaLand
  • 27
  • 6
  • 1
    I'm the author of Automate the Boring Stuff with Python: This program works by having the user copy text to the clipboard, then the program. The program will put the modified text back on the clipboard so the user can paste it. If you want output as well, you can add a print(text) line at the end. But this output isn't needed, it's only informational and cosmetic. – Al Sweigart Aug 21 '18 at 22:04
  • Oh, okay. Thanks for taking the time to respond. A quick question, what exactly is the clipboard? It's probably obvious I'm just a little slow. – MikaLand Aug 24 '18 at 03:37
  • The "clipboard" is a feature that operating systems provide where you can temporarily store text, files, or images. You can "copy" content to the clipboard from one program, then "paste" it into another program. This saves you from having to retype the text. Pyperclip offers a way for Python scripts to write text to ("copy") and read text from ("paste") the clipboard. – Al Sweigart Aug 24 '18 at 18:46
  • Okay now it makes sense. That's what I thought at first but I wasn't sure. Thanks for taking the time to respond. – MikaLand Aug 25 '18 at 19:34
  • I also had problems - But - The program works fine after I followed the procedure properly Al Sweigart had pointed out - My mistake was not keeping track of what I had copied on the clipboard. How2 - 1 Open Command Prompt (CMD) 2 Change directories to where you saved bulletPointAdder.py 3 Make a list in Notepad 4 Copy list in Notepad 5 In CMD (Now pointing to the director holding bulletPointAdder.py) and type bulletPointAdder.py 6 Hit Enter 7 Paste list on new Notepad 8 Bask in the glory of Python. – Peter Cockram Jul 21 '20 at 16:45

4 Answers4

1
import pyperclip
text  = pyperclip.paste()
text = text.split("\n")
for i in text:
    print( "* " + i)

-Above code is much simpler than the explained one in the book.

Testing Id
  • 11
  • 2
0

I'm not particularly familiar with pyperclip, but it seems like you're not telling pyperclip.paste() exactly what text you want to assign to the variable "text".

I looked at the documentation, and before you type "pyperclip.paste()", you need to enter "pyperclip.copy(text)" in order for something to be copied to the clipboard. Right now you are telling pyperclip to paste whatever is on the clipboard into text, but nothing is on the clipboard.

hope that helps.

UPDATE

I ran this program in Terminal, and it works:

#! python3
# bulletPointAdder.py - Adds Wikipedia bullet points to the start
# of each line of text on the clipboard.

import pyperclip
pyperclip.copy("Hello World")
text = pyperclip.paste()

# Separate lines and add stars.
lines = text.split('\n')
for i in range(len(lines)):    # loop through all indexes for "lines" list
    lines[i] = '* ' + lines[i] # add star to each string in "lines" list
text = '\n'.join(lines)
pyperclip.copy(text)
print(text)

OUTPUT:

* Hello World
ab123
  • 357
  • 4
  • 17
  • I tried adding some text to pyperclip.paste() and got an error unfortunately. – MikaLand Aug 06 '18 at 21:13
  • And another error: TypeError: lazy_load_stub_copy() missing 1 required positional argument: 'text' if I add pyperclip.copy() before the text variable – MikaLand Aug 06 '18 at 21:14
  • It appears that Stack Overflow automatically changed my answer before I posted it. You should type pyperclip.copy("Type your text here") – ab123 Aug 06 '18 at 21:22
  • The one positional argument that the error is referring to is the text you must insert into pyperclip.copy(), because otherwise nothing is being copied. – ab123 Aug 06 '18 at 21:23
  • Unfortunately that didn't work either. By the way, sorry for the wait, I've been busy. – MikaLand Aug 09 '18 at 22:48
  • Did you add quotes inside the copy function for your String? – ab123 Aug 12 '18 at 18:03
  • Yeah I tried. Strangely if I put the string in the copy function and then added print (text) below I get an output but I don't think that's how it's supposed to work. What do you think? – MikaLand Aug 14 '18 at 02:24
  • Well, if you assign a string like "Hello World" to text, even if you copy it, you can still call text in the print function. – ab123 Aug 14 '18 at 18:16
  • If you look above, I've added code that works in Terminal – ab123 Aug 14 '18 at 18:19
0

The code works fine. This is how I test it:

  1. Make a .txt file
  2. Make a bunch of lists items and save it
  3. Now copy your lists items
  4. Open your terminal with the path file and run your PointAdder.py
  5. Return or open another .txt file and paste, you should get your list with a * on each line
Bablu
  • 1
-1

The code is ok. This is how I test it:

  1. Make a example.txt file and write there a list of strings.

  2. Copy it (just like that with CTRL +C).

  3. Then make a bat file, name it bulletPointAdder.bat and inside paste the 2 @ lines with your path to py file and the second with pause. Save it.

  4. Now go to cmd (to your path to py file) and run bulletPointAdder.bat

  5. Go in example.txt file and do a paste (CTRL+P). Now you see the magic. The bat file converted the stings by adding the star in front)

Nico
  • 858
  • 10
  • 20
bveritoa
  • 1
  • 1