Python使用多剪贴板自动回复消息

Python编程快速上手——让繁琐工作自动化 第2版》

6.6 项目 使用多剪贴板自动回复消息

项目介绍详见书籍(需要配置python环境变量)

运行代码
python脚本(.py)
#! python3
# part6_object_6_6_base.py - A multi-clipborad program

import sys
import pyperclip

TEXT = {'agree': """Yes, I agree. That sounds fine to me.""",
        'busy': """Sorry, can we do this later this week or next week?""",
        'upsell': """Would you consider making this a monthly donation?"""
        }

if len(sys.argv) < 2:
    print('Usage: python mclip.py [keyphrase] - copy phrase text')
    sys.exit()

keyphrase = sys.argv[1]  # fist command line arg is the keyphrase

if keyphrase in TEXT:
    pyperclip.copy(TEXT[keyphrase])
    print('TEXT for ' + keyphrase + ' copied to clipboard.')
else:
    print('There is no text for ' + keyphrase)
批处理脚本(.bat)

脚本路径改为python脚本所在路径

REM @py.exe E:\pythonlearn\systematic_learning\automate_the_boring_stuff_with_python\automate_online-materials\mclip.py %*

REM @py -3.10 E:\pythonlearn\systematic_learning\automate_the_boring_stuff_with_python\automate_online-materials\mclip.py %*

@python.exe E:\pythonlearn\systematic_learning\automate_the_boring_stuff_with_python\automate_online-materials\mclip.py %*

@pause

将该bat脚本所在的位置加入到环境变量中,在windows操作系统上运行多剪贴板程序,就只要按win-R快捷键,再输入 mclip <关键词短语> 即可

C:\Users\SJV>mclip busy

C:\Users\SJV>REM @py.exe E:\pythonlearn\systematic_learning\automate_the_boring_stuff_with_python\automate_online-materials\mclip.py busy

C:\Users\SJV>REM @py -3.10 E:\pythonlearn\systematic_learning\automate_the_boring_stuff_with_python\automate_online-materials\mclip.py busy
TEXT for busy copied to clipboard.
请按任意键继续. . .
注意事项
1.python环境变量

        由于该脚本使用第三方库pyperclip,所以在pycharm项目和windows环境变量的python解释器中都要pip install pyperclip(如果为同一个解释器则无需重复安装),避免出现 ModuleNotFoundError: No module named 'pyperclip' 问题

        注:实际运行中,只有使用这种格式的命令行可以成功运行,使用@py.exe或@py或@py -3.10 均显示 ModuleNotFoundError: No module named 'pyperclip',暂时没有找到合适的解决方法

@python.exe E:\pythonlearn\systematic_learning\automate_the_boring_stuff_with_python\automate_online-materials\mclip.py %*

2.bat脚本编写
2.1 '@'的作用

        使用@前缀可以阻止该行命令在命令提示符窗口中显示

2.2 py.exe、py、python.exe的区别

        1.py.exe: 这是 Python 启动器,通常在 Windows 上安装 Python 时会自动安装。它允许你选择具体的 Python 版本来运行脚本。例如,py -3.10 script.py 会使用 Python 3.10 来运行 script.py。它还支持根据文件的 shebang (#! python3)行选择 Python 版本。

        2.py: 这是 py.exe 的简写,功能相同

        3.python.exe: 这是 Python 的具体解释器,通常在安装 Python 时在 PATH 环境变量中设置。直接调用 python.exe script.py 会使用你安装的默认 Python 版本来运行脚本。如果系统上安装了多个 Python 版本,需要通过完整路径来区分不同版本的解释器。

3.%*的使用

        %*表示将所有传递给批处理文件的参数传递给 Python 脚本。

4.pause的使用

        这行代码在批处理文件执行完毕后会暂停并显示“请按任意键继续...”的提示,直到用户按下任意键继续。这有助于在执行完脚本后查看输出或错误信息。

总结

项目本身python脚本的内容并不复杂,只是变量sys.argv的用法需要熟悉。

完成python脚本到 只要按win-R快捷键,再输入 mclip <关键词短语> 的过程中比预期要复杂,书中给的bat脚本示例代码为

@py.exe C:\path_to_file\mclip.py %*
@pause

如上文所述,在我的电脑上无法正常运行该bat脚本,会显示 ModuleNotFoundError: No module named 'pyperclip',暂时没有找到合适的解决方法,后面改为@python.exe的方法才成功运行。

ps:如果有老师知道使用@py.exe如何正常运行的方法,欢迎留言讨论,谢谢。

  • 14
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值