目标:
处理粘贴板中的内容,比如进行合并行、转换大小写、首字母大写等。
假设场景:
在阅读pdf文件的时候,遇见了需要复制的内容,一般复制下来都会有各种问题。比如换行错位。
那有没有办法把复制出来的内容,自动转换一下格式呢?比如把换行给去除掉,合并到一起、或者把复制出来的英文首字母大写其他小写,看起来更好看一点?
先看效果:
1、在pdf文件中复制一段话出来。
2、直接粘贴到txt文本文档里
3、点击用Python写的小工具的功能,来处理粘贴板的内容
4、再粘贴到txt文本文档中
5、步骤2和步骤4效果兑对比。发现确实把换行符什么的给去掉了,相当于合并行。
这样就实现了复制后格式化粘贴板内容的功能。
那代码怎么写?(源代码出自:程序员二山(抖音46734750903)我在原有基础上加了点东西)
如下:
import tkinter as tk # python 标准库提供的图形化界面
import clipboard
"""
图形化界面:对交互式要求比较强
- office
- adobe
命令行界面: 常用于服务器端
- linux/unix
"""
def to_title():
"""
1. 从剪贴板获取文本数据
2. 将文本数据更改为标题形式(返回"标题化"的 string,就是说所有单词都是以大写开始,其余字母均为小写(见 istitle()))
3. 把处理过的文本放到剪贴板里面
"""
text = clipboard.paste()
output = text.title()
print('处理完成后的数据',output)
clipboard.copy(output)
def merge_lines():
"""
1. 从剪贴板获取文本数据
2. 将文本数据里的 \r\n or \n 给替换掉
3. 把处理过的文本放到剪贴板里面
"""
text = clipboard.paste()
print('拷贝的数据:',text)
end = "\r\n" if "\r\n" in text else "\n"
output = text.replace(end, "")
print('处理完成后的数据', output)
clipboard.copy(output)
def to_lower():
"""
转换为小写
"""
text = clipboard.paste()
print('拷贝的数据:',text)
output = str(text).lower()
print('处理完成后的数据', output)
clipboard.copy(output)
def to_upper():
"""
转换为大写
"""
text = clipboard.paste()
print('拷贝的数据:',text)
output = str(text).upper()
print('处理完成后的数据', output)
clipboard.copy(output)
def first_upper():
"""
首字母大写
"""
text = clipboard.paste()
print('拷贝的数据:',text)
output = str(text).capitalize()
print('处理完成后的数据',output)
clipboard.copy(output)
def pipeline(process=lambda x: x):
"""
1. 从剪贴板获取文本数据
2. 处理文本数据
3. 把处理过的文本放到剪贴板里面
"""
text = clipboard.paste()
print('拷贝的数据:',text)
output = process(text)
print('处理完成后的数据', output)
clipboard.copy(output)
def ml(text: str):
"""Merge Lines"""
end = "\r\n" if "\r\n" in text else "\n"
output = text.replace(end, "")
return output
def main():
pipeline(ml)
if __name__ == "__main__":
# 图形化界面的一个解决方式
# 列表 list -> 最常用的容器数据结构
# 定义非常简单,[]
text_li = ["首字母大写其他小写", "合并行", "转为小写", "转为大写", "首字母大写"]
func_li = [to_title, merge_lines, to_lower, to_upper, first_upper]
# func_li = [to_title, merge_lines, to_lower(), to_upper(), first_upper()]
window = tk.Tk()
window.title('处理粘贴板的小工具')
window.maxsize(500,500)
window.minsize(300,300)
# [exp for each in li] exp -> 最终元素的形态
# [tk.Button(), tk.Button(), ...]
# buttons_copy = []
# for text, func in zip(text_li, func_li):
# buttons_copy.append(tk.Button(text=text, command=func))
buttons = [tk.Button(text=t, command=f) for t, f in zip(text_li, func_li)]
# for i, j in zip(text_li, func_li):
# print(i, j)
# for but in buttons:
# but.pack()
ret = [but.pack() for but in buttons] # -> [None, None]
exit_but = tk.Button(text="退出", command=window.destroy)
exit_but.pack()
window.mainloop()
运行后,会生成一个Windows窗体。
使用:
1、在复制一段文字或者英文
2、在Windows窗体上,选择需要做的操作
3、粘贴到想要粘贴到的地方
4、over
扩展:
这个功能的本质,是处理字符串,只不过处理的是粘贴板里的字符串。
所以可以写很多按钮和对应的功能来进行扩展。
比如代码里用到的lower(),upper(),title()等等都是Python里内置的字符串函数。甚至可以自己写代码处理字符串。
学习这段代码时候遇到的问题:
1、最开始源码里只有这两个函数:
to_title(), merge_lines()
后来我自己加了3个:
to_lower(), to_upper(), first_upper()
加完代码是这样的:
func_li = [to_title, merge_lines, to_lower(), to_upper(), first_upper()]
运行后,发现问题:
to_title(), merge_lines()可以正常使用
to_lower(), to_upper(), first_upper()不可以正常使用
后来找大神帮忙解答,答案如下:
列表可以存储函数。但是注意:存储函数地址和调用函数的区别。
比如这2句话:
func_li = [to_title, merge_lines, to_lower, to_upper, first_upper]
func_li = [to_title, merge_lines, to_lower(), to_upper(), first_upper()]
这2句话区别在于函数名后加没加括号:to_title------------------to_lower()
那么加不加括号的区别是什么呢?
to_title:没加括号,就是把函数地址存入list
to_lower():加了括号,就是调用函数
运行效果也不一样。
比如下图:to_title返回了调用地址;而to_lower()返回了此函数的调用结果:None(因为这函数定义的时候就没返回值,所以就返回了个None)
另外:有兴趣的可以多研究扩展下用途。
比如,经常复制粘贴pdf的,对合并行用的比较多,但是每次都去那个Windows窗体点一下,也挺麻烦的。那么有没有办法,复制之后直接粘贴就是合并行之后的结果?
答案是可以的。写一个while true循环就行。
比如,复制的文字里,有中英文,但是我只想要英文,能不能自动去除掉中文,只保留英文?
也可以。比如用
isascii
欢迎补充
说明:
源代码出自:程序员二山(抖音46734750903)。