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

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



Windows中定时任务

定时任务最主要的功能 ,就是帮助我们在指定的时间运行某个程序或者脚本。比如:

1、定期发送邮件

2、定期统计工资情况

3、定期汇总文件

4、定期清理文件

Windows进入定时任务的方法为: 右键“此电脑”-----> 管理 -----> 系统工具 -----> 任务计划程序

快捷键

Windows 键+R,调出"运行窗口",输入 compmgmt.msc

OS/linux 系统定时任务

crontab

1、-e 编辑工作表

2、-l 列出工作表里的命令

3、-r 删除工作表

crontab 的命令构成为 时间+动作,其时间有分、时、日、月、周 五种,操作符有

1、取值范围内的所有数字

2、/ 每过多少个数字

3、- 从 X 到 Z

4、散列数字

注意

crontab 新增了任务后,重启下服务,命令如下: /etc/init.d/cron restart

案例

实例 1:每 1 分钟执行一次 myCommand

\* * * * * myCommand  

实例 2:每小时的第 3 和第 15 分钟执行

3,15 * * * * myCommand  

实例 3:在上午 8 点到 11 点的第 3 和第 15 分钟执行

3,15 8-11 * * * myCommand  

实例 4:每隔两天的上午 8 点到 11 点的第 3 和第 15 分钟执行

3,15 8-11 */2 * * myCommand

实例 5:每周一上午 8 点到 11 点的第 3 和第 15 分钟执行

3,15 8-11 * * 1 myCommand

实例 6:每晚的 21:30 重启 smb

30 21 * * * /etc/init.d/smb restart

实例 7:每月 1、10、22 日的 4 : 45 重启 smb

45 4 1,10,22 * * /etc/init.d/smb restart

实例 8:每周六、周日的 1 : 10 重启 smb

10 1 * * 6,0 /etc/init.d/smb restart

实例 9:每天 18 : 00 至 23 : 00 之间每隔 30 分钟重启 smb

0,30 18-23 * * * /etc/init.d/smb restart

实例 10:每星期六的晚上 11 : 00 pm 重启 smb

0 23 * * 6 /etc/init.d/smb restart

实例 11:每一小时重启 smb

\* */1 * * * /etc/init.d/smb restart

实例 12:晚上 11 点到早上 7 点之间,每隔一小时重启 smb

\* 23-7/1 * * * /etc/init.d/smb restart

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
目录
相关文章
|
17天前
|
监控 Unix Linux
Linux操作系统调优相关工具(四)查看Network运行状态 和系统整体运行状态
Linux操作系统调优相关工具(四)查看Network运行状态 和系统整体运行状态
31 0
|
19天前
|
算法 程序员
深入理解操作系统内存管理:分页系统的优势与挑战
【4月更文挑战第7天】 在现代操作系统中,内存管理是一项至关重要的任务,它确保了计算机能够高效、安全地运行各种程序。分页系统作为内存管理的一种技术,通过将物理内存分割成固定大小的单元——页面,为每个运行的程序提供了一种独立且连续的内存地址空间。该技术不仅简化了内存分配,还允许更高效的内存使用和保护。本文探讨了分页系统的核心原理,优势以及面临的挑战,旨在为读者揭示其在操作系统设计中的重要性。
|
4天前
|
Unix Linux Windows
Linux的学习之路:3、基础指令(2)
Linux的学习之路:3、基础指令(2)
28 0
|
4天前
|
存储 Linux API
软件体系结构 - 嵌入式系统(2)- 嵌入式操作系统
软件体系结构 - 嵌入式系统(2)- 嵌入式操作系统
10 0
|
3天前
|
前端开发 Android开发 开发者
移动应用与系统:探索开发实践与操作系统融合
【4月更文挑战第23天】 在数字化时代的浪潮中,移动应用与系统的发展已成为技术创新和商业竞争的前沿阵地。本文将深入探讨移动应用的开发实践、移动操作系统的最新趋势以及二者之间的紧密融合。通过分析跨平台工具的效能、原生开发的优化策略以及操作系统的定制化功能,旨在为开发者提供一个全面而深入的视角,以适应不断变化的市场需求和技术挑战。
|
4天前
|
开发者 Python
Python的os模块详解
Python的os模块详解
15 0
|
4天前
|
消息中间件 Unix Linux
Linux的学习之路:17、进程间通信(1)
Linux的学习之路:17、进程间通信(1)
20 1
|
4天前
|
存储 安全 Linux
Linux的学习之路:9、冯诺依曼与进程(1)
Linux的学习之路:9、冯诺依曼与进程(1)
18 0
|
16天前
|
监控 测试技术 Android开发
移动应用与系统:开发与操作系统的深度解析
【4月更文挑战第11天】在这篇文章中,我们将深入探讨移动应用的开发过程,以及移动操作系统如何影响这些应用的性能和功能。我们将详细分析移动应用开发的关键步骤,包括需求分析、设计、编码、测试和维护。同时,我们也将探讨移动操作系统,如Android和iOS,如何为应用开发提供支持,并影响其性能。
|
17天前
|
Linux
Linux Crontab 查看定时任务启动没
Linux Crontab 查看定时任务启动没
16 0
Linux Crontab 查看定时任务启动没