Python 自动化无聊的东西:要点不能 运行 程序

Python Automate the Boring Stuff: Bullet Points can't run program

好吧,我正在研究第 6 章,将无聊的东西自动化,但我无法理解如何将它应用到 运行 项目中。它 运行s 但出现的只是 "Press any key to continue..."。就好像我不能输入和串起来让它工作……或者至少我认为它应该是这样的。我还不是 pyperclip 的最佳人选,也不是 运行 的最佳人选。

任何人都可以帮助我了解如何让它工作,以便我可以得到一些输出吗?我也不确定如何在 cmd 行中使用剪贴板,有什么想法吗?

#! 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)

这是我正在使用的 bin 文件:

 @py C:\Users\david\MyPythonScripts\AddingBullets.py %*
@pause

我对 pyperclip 不是特别熟悉,但你似乎没有告诉 pyperclip.paste() 你想要分配给变量 "text".[=12= 的确切文本]

我查看了文档,在键入 "pyperclip.paste()" 之前,您需要输入 "pyperclip.copy(text)" 才能将某些内容复制到剪贴板。现在你告诉 pyperclip 将剪贴板上的任何内容粘贴到文本中,但剪贴板上没有任何内容。

希望对您有所帮助。

更新

我在终端中 运行 这个程序,它工作:

#! 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)

输出:

* Hello World

代码没问题。这是我测试它的方式:

  1. 创建一个 example.txt 文件并在其中写入一个字符串列表。

  2. 复制它(就像使用 CTRL +C 一样)。

  3. 然后制作一个 bat 文件,将其命名为 bulletPointAdder.bat 并在里面粘贴 2 @ 行,其中包含您的 py 文件路径,第二行带有暂停。保存。

  4. 现在转到 cmd(到你的 py 文件路径)和 运行 bulletPointAdder.bat

  5. 进入 example.txt 文件并粘贴 (CTRL+P)。现在你看到了魔法。 bat文件通过在前面加星号来转换stings)

代码运行良好。这是我测试它的方式:

  1. 创建一个.txt文件
  2. 制作一堆列表项并保存
  3. 现在复制您的列表项
  4. 使用 path file 和 运行 打开您的终端 PointAdder.py
  5. Return 或打开另一个 .txt 文件并粘贴,您应该得到每行有一个 * 的列表
import pyperclip
text  = pyperclip.paste()
text = text.split("\n")
for i in text:
    print( "* " + i)

-上面的代码比书上讲解的简单多了