📜  Python – 使用 Pyperclip 模块将简单行转换为项目符号行

📅  最后修改于: 2022-05-13 01:54:22.864000             🧑  作者: Mango

Python – 使用 Pyperclip 模块将简单行转换为项目符号行

Pyperclip是跨平台的Python模块,用于将文本复制和粘贴到剪贴板。假设您要自动执行复制文本的任务,然后将它们转换为项目符号点。这可以使用pyperclip模块来完成。请考虑以下示例以更好地理解。

例子:

方法:首先,您需要复制要转换为项目符号文本的任何文本,然后您需要运行程序,成功执行后,当您粘贴剪贴板的内容时,您可以看到内容已被修改。该模块的两种方法已被使用,即用于粘贴和返回复制字符串的paste()方法和用于将字符串传递到剪贴板的copy()方法。

下面是实现。

import pyperclip
   
  
# saves text copied to clipboard
# in variable text
text = pyperclip.paste()
print("Before Modification:")
print(text)
   
# stores different lines of text
# in a list named lines
lines = text.split("\n")
   
# adds * to every line stored
# in list
for i in range(len(lines)):
    lines[i] = "*" + lines[i]
  
# converts the list of different
# lines to single text
text = "\n".join(lines)
   
# copies new modified text
# to the clipboard
pyperclip.copy(text)
print("\nAfter Modification:") 
print(pyperclip.paste())
输出:
Before Modification:
United we stand, divided we fall.
Where there is a will, there is a way.
Rome was not built in a day.

After Modification:
*United we stand, divided we fall.
*Where there is a will, there is a way.
*Rome was not built in a day.