python实践项目五:操作剪贴板-pyperclip模块

描述:读取剪贴板的内容,修改该内容,再将修改后的内容重新写进剪贴板

注意:执行程序代码前需保证剪贴板有内容,可复制以下内容来测试:

Lists of animals

Lists of aquarium life

Lists of biologists by author abbreviation

Lists of cultivars

代码

 1 #!/usr/bin/python
 2 # -*- coding: UTF-8 -*-
 3 #执行程序之前剪贴板的内容:
 4 '''
 5 Lists of animals
 6 Lists of aquarium life
 7 Lists of biologists by author abbreviation
 8 Lists of cultivars
 9 '''
10 #执行程序之后剪贴板的内容:
11 '''
12 * Lists of animals
13 * Lists of aquarium life
14 * Lists of biologists by author abbreviation
15 * Lists of cultivars
16 '''
17 import pyperclip
18 text = pyperclip.paste()#获得剪贴板的内容,返回一个字符串
19 print '之前的剪贴板内容:\n',text
20 lines = text.split('\n')  #以‘\n’为分隔符分割字符串,并返回一个列表,参考资料:https://www.runoob.com/python/att-string-split.html
21 for i in range(len(lines)):
22     lines[i] = '* ' + lines[i]
23     newtext = '\n'.join(lines) #用‘\n’连接列表的每一个元素,并返回一个字符串,参考链接:https://www.runoob.com/python/att-string-join.html
24 pyperclip.copy(newtext) #将text写入剪贴板
25 print '执行程序后的剪贴板内容:\n',newtext

运行结果:

 

posted on 2019-07-05 17:41  白居不易1101  阅读(764)  评论(1编辑  收藏  举报