2

好的,所以我正在编写第 6 章关于自动化无聊的东西,但我无法理解如何让它运行项目。它运行,但出现的只是“按任意键继续...”。就好像我不能输入和字符串让它工作......或者至少我认为它应该是这样的。我不是最好的 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
4

4 回答 4

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

- 上面的代码比书中解释的要简单得多。

于 2020-06-04T11:06:22.483 回答
0

我对 pyperclip 不是特别熟悉,但似乎您并没有确切地告诉 pyperclip.paste() 要将什么文本分配给变量“文本”。

我查看了文档,在您输入“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
于 2018-08-06T20:53:21.567 回答
0

代码工作正常。这就是我测试它的方式:

  1. 制作.txt文件
  2. 制作一堆列表项并保存
  3. 现在复制您的列表项
  4. 打开你的终端path file并运行你的PointAdder.py
  5. 返回或打开另一个.txt文件并粘贴,你应该得到你的列表,*每行都有一个
于 2020-04-03T04:55:54.300 回答
-1

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

  1. 制作一个 example.txt 文件并在那里写一个字符串列表。

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

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

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

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

于 2019-04-03T19:54:50.630 回答