Python办公自动化【Windows中定时任务、OS/linux 系统定时任务 、Python 钉钉发送消息、Python 钉钉发送图片】(九)-全面详解(学习总结---从入门到深化)(下)

简介: Python办公自动化【Windows中定时任务、OS/linux 系统定时任务 、Python 钉钉发送消息、Python 钉钉发送图片】(九)-全面详解(学习总结---从入门到深化)

Python办公自动化【Windows中定时任务、OS/linux 系统定时任务 、Python 钉钉发送消息、Python 钉钉发送图片】(九)-全面详解(学习总结---从入门到深化)(上):https://developer.aliyun.com/article/1420359


Python 钉钉发送消息



创建钉钉机器人


安装

pip install dingtalkchatbot


常用方法与属性

函数名&属性 含义
dingtalkchatbot.chatbot.DingtalkChatbot  创建机器人
chatbot.send_text()  发送消息
chatbot.send_image()  发送图片
chatbot.send_link() 发送链接
chatbot.send_markdown() 发送Markdown文档


代码

from dingtalkchatbot.chatbot import DingtalkChatbot
def send_text():
 # 创建一个机器人
  webhook = 'https://oapi.dingtalk.com/robot/send?access_token=5a6e68c7a3c146c4b37126cbd434ffaf6b11bc7763ac91136130a38dbd25ae83'
  secret = 'SEC63299e56a96cb2a24b9b67834bc9df3498963f101e9800a9fa7bdc76c3240228'
  bot = DingtalkChatbot(webhook,secret)
  # 发送消息
  # bot.send_text('大家好~我是小童小助手~')
  # bot.send_text('大家该交作业啦~')
  # bot.send_text('要开会啦~',is_at_all=True)
  bot.send_text('你之前问题搞定了没有?',at_mobiles=[1668888888])
if __name__ =='__main__':
  send_text()


Python 钉钉发送图片



常用方法与属性

函数名&属性  含义
dingtalkchatbot.chatbot.DingtalkChatbot 创建机器人
chatbot.send_image() 发送图片


代码

from dingtalkchatbot.chatbot import DingtalkChatbot
def send_img():
  webhook = 'https://oapi.dingtalk.com/robot/send?access_token=5a6e68c7a3c146c4b37126cbd434ffaf6b11bc7763ac91136130a38dbd25ae83'
  secret = 'SEC63299e56a96cb2a24b9b67834bc9df3498963f101e9800a9fa7bdc76c3240228'
  # 创建机器人
  bot = DingtalkChatbot(webhook,secret)
  # 发送图片消息
 bot.send_image('https://www.itbaizhan.com/public/new/index/images/courimg2.jpg')
if __name__ =='__main__':
  send_img()


Python钉钉发送链接与MarkDown



常用方法与属性

函数名&属性  含义
dingtalkchatbot.chatbot.DingtalkChatbot 创建机器人
chatbot.send_link()  发送链接
chatbot.send_markdown()  发送Markdown文档


代码

from dingtalkchatbot.chatbot import DingtalkChatbot
def send_link():
  webhook = 'https://oapi.dingtalk.com/robot/send?access_token=5a6e68c7a3c146c4b37126cbd434ffaf6b11bc7763ac91136130a38dbd25ae83'
  secret = 'SEC63299e56a96cb2a24b9b67834bc9df3498963f101e9800a9fa7bdc76c3240228'
  # 创建机器器
 chart_bot = DingtalkChatbot(webhook,secret)
  # 发送链接
  chart_bot.send_link(
    title='Python自动化课程',
    text='欢迎学习Python课程',
  message_url='http://www.baidu.cn',
 pic_url='https://www.baidu.com/public/new/index/images/courimg3.jpg')
def send_markdown():
  webhook = 'https://oapi.dingtalk.com/robot/send?access_token=5a6e68c7a3c146c4b37126cbd434ffaf6b11bc7763ac91136130a38dbd25ae83'
  secret = 'SEC63299e56a96cb2a24b9b67834bc9df3498963f101e9800a9fa7bdc76c3240228'
  # 创建机器器
  chart_bot = DingtalkChatbot(webhook,secret)
  # 发送markdown
  chart_bot.send_markdown(
    title='Python自动化课程',
    text='### 欢迎学习Python课程\n'
    '此课程我们安排了word、excle、ptt 等.....'
    '![Python] (https://www.baidu.com/public/new/index/images/couimg1.jpg)' )
if __name__ =='__main__':
  # send_link()
  send_markdown()


Python钉钉发送卡片消息



常用方法与属性

函数名&属性 含义
dingtalkchatbot.chatbot.DingtalkChatbot  创建机器人
dingtalkchatbot.chatbot.CardItem(title,url,pic_url)  卡片消息对象
chatbot.send_feed_card()  发送卡片消息


代码

from dingtalkchatbot.chatbot import DingtalkChatbot
from dingtalkchatbot.chatbot import CardItem
def send_card():
  webhook = 'https://oapi.dingtalk.com/robot/send?
access_token=5a6e68c7a3c146c4b37126cbd434ffaf6b11bc7763ac91136130a38dbd25ae83'
  secret = 'SEC63299e56a96cb2a24b9b67834bc9df3498963f101e9800a9fa7bdc76c3240228'
  # 创建机器人
  chat_bot = DingtalkChatbot(webhook,secret)
  # 创建卡片对象
  card1 = CardItem(title='氧眼美女',url='http://www.baidu.cn',pic_url='https://pic.netbian.com/uploads/allimg/220511/003034-16522002340d03.jpg')
  card2 = CardItem(title='氧眼风景',url='http://www.baidu.cn',pic_url='https://pic.netbian.com/uploads/allimg/180826/113958-1535254798fc1c.jpg')
  card3 = CardItem(title='氧眼漫画',url='http://www.baidu.cn',pic_url='https://pic.netbian.com/uploads/allimg/210831/102129-16303764895142.jpg')
  # 发送卡片消息
 chat_bot.send_feed_card([card1,card2,card3]
)
if __name__ =='__main__':
  send_card()


Python钉钉发送互动卡片消息



常用方法与属性

函数名&属性 含义
dingtalkchatbot.chatbot.DingtalkChatbot  创建机器人
dingtalkchatbot.chatbot.ActionCard(title,text,btns)  互动类型卡片消息对象
dingtalkchatbot.chatbot.CardItem(title,url)  卡片消息对象
chatbot.send_action_card()  发送互动卡片消息


代码

from dingtalkchatbot.chatbot import DingtalkChatbot,CardItem
from dingtalkchatbot.chatbot import ActionCard
def send_card():
  webhook = 'https://oapi.dingtalk.com/robot/send?access_token=5a6e68c7a3c146c4b37126cbd434ffaf6b11bc7763ac91136130a38dbd25ae83'
  secret = 'SEC63299e56a96cb2a24b9b67834bc9df3498963f101e9800a9fa7bdc76c3240228'
  # 创建机器人
  chat_bot = DingtalkChatbot(webhook,secret)
  # 创建一个选项
  card1 = CardItem(title='查看详情',url='http://www.baidu.cn')
  # 创建互动卡片对象
  action_card = ActionCard(
    title='万万没想到,竟然...',
    text='![选择] (https://pic.netbian.com/uploads/allimg/210317/001935-16159115757f04.jpg) \n### 故事是这样子的...',
    btns=(card1,),
    btn_orientation = 1,   # 只有2个按钮的时候好用
    hide_avatar=1
 )
  # 发送卡片消息
  chat_bot.send_action_card(action_card)
def send_card2():
  webhook = 'https://oapi.dingtalk.com/robot/send?access_token=5a6e68c7a3c146c4b37126cbd434ffaf6b11bc7763ac91136130a38dbd25ae83'
  secret = 'SEC63299e56a96cb2a24b9b67834bc9df3498963f101e9800a9fa7bdc76c3240228'
  # 创建机器人
  chat_bot = DingtalkChatbot(webhook,secret)
  # 创建一个选项
  card1 = CardItem(title='支持',url='http://www.baidu.cn')
  card2 = CardItem(title='反对',url='http://www.baidu.cn')
  # 创建互动卡片对象
  action_card = ActionCard(
    title='万万没想到,竟然...',
    text='![选择] (https://pic.netbian.com/uploads/allimg/210317/001935-16159115757f04.jpg) \n### 故事是这样子的...',
    btns=(card1,card2),
    btn_orientation = 1,   # 只有2个按钮的时候好用
    hide_avatar=1
 )
  # 发送卡片消息
  chat_bot.send_action_card(action_card)
def send_card3():
   webhook = 'https://oapi.dingtalk.com/robot/send?access_token=5a6e68c7a3c146c4b37126cbd434ffaf6b11bc7763ac91136130a38dbd25ae83'
  secret = 'SEC63299e56a96cb2a24b9b67834bc9df3498963f101e9800a9fa7bdc76c3240228'
  # 创建机器人
  chat_bot = DingtalkChatbot(webhook,secret)
  # 创建一个选项
  card1 = CardItem(title='支持',url='http://www.baidu.cn')
  card2 = CardItem(title='弃票',url='http://www.baidu.cn')
  card3 = CardItem(title='反对',url='http://www.baidu.cn')
  # 创建互动卡片对象
  action_card = ActionCard(
    title='万万没想到,竟然...',
    text='![选择] (https://pic.netbian.com/uploads/allimg/190824/212516-1566653116f355.jpg) \n### 故事是这样子的...',
    btns=(card1,card2,card3),
    btn_orientation = 1,  # 只有2个按钮的时候好用
    hide_avatar=1 )
  # 发送卡片消息
  chat_bot.send_action_card(action_card)
if __name__ =='__main__':
  # send_card()
  # send_card2()
  send_card3()


操作压缩文件 - ZipFile



常用方法与属性

函数名&属性 含义
ZipFile(file,model)  打开压缩文件,w压缩,r解压,a追加
zipFile.write(file)  增加要压缩的文件
zipFile.namelist()  显示压缩包的文件
zipFile.extractall() 解压所有文件


代码

from zipfile import ZipFile
def zip_in():
  # 创建压缩文件
  with
ZipFile('./create_data/12_zip_in.zip','w')
as f:
    # 增加压缩文件
    f.write('./06_task.py')
    f.write('./05_zmail的使用.py')
def zip_out():
 # 打开压缩文件
  with
ZipFile('./create_data/12_zip_in.zip','r')
as f:
    # 读取数据
    print(f.namelist())
    # 解压数据
    f.extractall('./create_data')
if __name__ =='__main__':
  # zip_in()
  zip_out()


Python压缩文件工具开发



需求:给一个指定路径,将其制做成压缩文件


1、路径为文件,直接压缩

2、路径为文件夹,遍历文件夹文件压缩

import os
from zipfile import ZipFile
def new_file(base_path,zip_path):
  # 创建一个压缩文件
  with ZipFile(zip_path,'w') as zip:
    # 通过os.path.isfile() 判断是否是文件
    if os.path.isfile(base_path):
      zip.write(base_path) 
    else:
      # 遍历文件夹
      for root,dirs,files in os.walk(base_path):
        # root 遍历的当前文件夹名称
        # dirs 当前路径包含的文件夹
       # files 当前路径包含的文件
        for f in files:
 zip.write(os.path.join(root,f))
# 在压缩包追加新文件
def add_file(base_path,zip_path):
  # 创建一个压缩文件
  with ZipFile(zip_path,'a') as zip:
    # 通过os.path.isfile() 判断是否是文件
    if os.path.isfile(base_path):
      zip.write(base_path) 
    else:
      # 遍历文件夹
      for root,dirs,files in os.walk(base_path):
        # root 遍历的当前文件夹名称
        # dirs 当前路径包含的文件夹
        # files 当前路径包含的文件
        for f in files:
 zip.write(os.path.join(root,f))
if __name__ =='__main__':
  add_file('05_zmail的使用.py','./create_data/13_压缩工具.zip')


操作压缩文件 - tarfile



常用方法与属性

函数名&属性  含义
tarfile.open(path,model)  打开/创建压缩的文件,model: 'w'写,'r'读,'a'追加
tar.add(path) 增加压缩文件
tar.getmembers()  显示所有的文件信息
tar.extractall()  解压所有文件


代码

import tarfile
def tar_in():
  # 创建压缩文件
  with
tarfile.open('./create_data/14_tarfile.tar','w') as tar:
    # 增加压缩文件
    tar.add('11_发送钉钉互动卡片消息.py')
    tar.add('10_发送钉钉卡片消息.py')
def tar_out():
  # 打开压缩文件
  with
tarfile.open('./create_data/14_tarfile.tar','r') as tar:
    # 获取压缩包里的数据
    # print(tar.getmembers())
    # 解压压缩文件
    tar.extractall('./create_data')
if __name__=='__main__':
  # tar_in()
  tar_out()


暴力破解压缩密码



代码

from zipfile import ZipFile
import os
def passwd(path,pwd):
  type_ = os.path.splitext(path)[-1][1:]
  if type_ == 'zip':
    with ZipFile(path,'r') as zip:
      print(f'正在尝试密码: {pwd}')
      try:
       zip.extractall('./create_data/生成压缩文件',pwd=str(pwd).encode('utf-8'))
        print(f'解压成功,密码是:{pwd}')
        return True
      except Exception as e:
        pass
def create_pwd(length):
  import itertools as its
  words='1234567890asd'
  for i in range(1,length):
    base = its.product(words,repeat=i)
    for i in base:
      yield ''.join(i)
if __name__ == "__main__":
  # passwd('./base_data/aa.zip')
  for p in create_pwd(5):
    flag = passwd('./base_data/aa.zip',p)
    if flag:
      break
目录
相关文章
|
7月前
|
存储 JavaScript Java
(Python基础)新时代语言!一起学习Python吧!(四):dict字典和set类型;切片类型、列表生成式;map和reduce迭代器;filter过滤函数、sorted排序函数;lambda函数
dict字典 Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map,使用键-值(key-value)存储,具有极快的查找速度。 我们可以通过声明JS对象一样的方式声明dict
434 2
|
7月前
|
存储 Java 数据处理
(numpy)Python做数据处理必备框架!(一):认识numpy;从概念层面开始学习ndarray数组:形状、数组转置、数值范围、矩阵...
Numpy是什么? numpy是Python中科学计算的基础包。 它是一个Python库,提供多维数组对象、各种派生对象(例如掩码数组和矩阵)以及用于对数组进行快速操作的各种方法,包括数学、逻辑、形状操作、排序、选择、I/0 、离散傅里叶变换、基本线性代数、基本统计运算、随机模拟等等。 Numpy能做什么? numpy的部分功能如下: ndarray,一个具有矢量算术运算和复杂广播能力的快速且节省空间的多维数组 用于对整组数据进行快速运算的标准数学函数(无需编写循环)。 用于读写磁盘数据的工具以及用于操作内存映射文件的工具。 线性代数、随机数生成以及傅里叶变换功能。 用于集成由C、C++
603 1
|
7月前
|
算法 Java Docker
(Python基础)新时代语言!一起学习Python吧!(三):IF条件判断和match匹配;Python中的循环:for...in、while循环;循环操作关键字;Python函数使用方法
IF 条件判断 使用if语句,对条件进行判断 true则执行代码块缩进语句 false则不执行代码块缩进语句,如果有else 或 elif 则进入相应的规则中执行
1244 1
|
7月前
|
Linux 虚拟化 iOS开发
VMware Remote Console 13.0.1 for macOS, Linux, Windows - vSphere 虚拟机控制台的桌面客户端
VMware Remote Console 13.0.1 for macOS, Linux, Windows - vSphere 虚拟机控制台的桌面客户端
1639 0
VMware Remote Console 13.0.1 for macOS, Linux, Windows - vSphere 虚拟机控制台的桌面客户端
|
7月前
|
存储 Java 索引
(Python基础)新时代语言!一起学习Python吧!(二):字符编码由来;Python字符串、字符串格式化;list集合和tuple元组区别
字符编码 我们要清楚,计算机最开始的表达都是由二进制而来 我们要想通过二进制来表示我们熟知的字符看看以下的变化 例如: 1 的二进制编码为 0000 0001 我们通过A这个字符,让其在计算机内部存储(现如今,A 字符在地址通常表示为65) 现在拿A举例: 在计算机内部 A字符,它本身表示为 65这个数,在计算机底层会转为二进制码 也意味着A字符在底层表示为 1000001 通过这样的字符表示进行转换,逐步发展为拥有127个字符的编码存储到计算机中,这个编码表也被称为ASCII编码。 但随时代变迁,ASCII编码逐渐暴露短板,全球有上百种语言,光是ASCII编码并不能够满足需求
320 4
|
8月前
|
JavaScript Java 大数据
基于python的网络课程在线学习交流系统
本研究聚焦网络课程在线学习交流系统,从社会、技术、教育三方面探讨其发展背景与意义。系统借助Java、Spring Boot、MySQL、Vue等技术实现,融合云计算、大数据与人工智能,推动教育公平与教学模式创新,具有重要理论价值与实践意义。
|
8月前
|
安全 Linux 网络安全
Nipper 3.9.0 for Windows & Linux - 网络设备漏洞评估
Nipper 3.9.0 for Windows & Linux - 网络设备漏洞评估
224 0
Nipper 3.9.0 for Windows & Linux - 网络设备漏洞评估
|
8月前
|
机器学习/深度学习 编解码 Python
Python图片上采样工具 - RealESRGANer
Real-ESRGAN基于深度学习实现图像超分辨率放大,有效改善传统PIL缩放的模糊问题。支持多种模型版本,推荐使用魔搭社区提供的预训练模型,适用于将小图高质量放大至大图,放大倍率越低效果越佳。
637 3
|
8月前
|
机器学习/深度学习 文字识别 Java
Python实现PDF图片OCR识别:从原理到实战的全流程解析
本文详解2025年Python实现扫描PDF文本提取的四大OCR方案(Tesseract、EasyOCR、PaddleOCR、OCRmyPDF),涵盖环境配置、图像预处理、核心识别与性能优化,结合财务票据、古籍数字化等实战场景,助力高效构建自动化文档处理系统。
2047 0

热门文章

最新文章