Python处理办公自动化的十大场景(下)

上一篇 / 下一篇  2022-06-07 11:03:30

  6. Python控制鼠标
  这是很多人的需求,实现对鼠标的自动控制,去做一些流水线的工作,比如软件测试
  Python有个pyautogui库可以任意地去控制你的鼠标。
  控制鼠标左击/右击/双击函数以及测试源码:
  import pyautogui as pg
  try:
      while True:
          x, y = pg.position()
          print(str(x) + " " + str(y))  #输出鼠标位置
          if 1746 < x < 1800 and 2 < y < 33:
              pg.click()#左键单击
          if 1200 < x < 1270 and 600 < y < 620:
              pg.click(button='right')#右键单击
          if 1646 < x < 1700 and 2 < y < 33:
              pg.doubleClick()#左键双击
  except KeyboardInterrupt:
      print("\n")

  7. Python控制键盘
  同样的,Python也可以通过pyautogui控制键盘。
  键盘写入:
  import pyautogui
  #typewrite()无法输入中文内容,中英文混合的只能输入英文
  #interval设置文本输入速度,默认值为0
  pyautogui.typewrite('你好,world!',interval=0.5)

  8. Python压缩文件
  压缩文件是办公中常见的操作,一般压缩会使用压缩软件,需要手动操作。
  Python中有很多包支持文件压缩,可以让你自动化压缩或者解压缩本地文件,或者将内存中的分析结果进行打包。比如zipfile、zlib、tarfile等可以实现对.zip、.rar、.7z等压缩文件格式的操作。
  压缩文件:
  import zipfile
  try:
    with zipfile.ZipFile("c://test.zip",mode="w") as f:
      f.write("c://test.txt")          #写入压缩文件,会把压缩文件中的原有覆盖
  except Exception as e:
      print("异常对象的类型是:%s"%type(e))
      print("异常对象的内容是:%s"%e)
  finally:
      f.close()

  解压文件:
  import zipfile
  try:
    with zipfile.ZipFile("c://test.zip",mode="a") as f:
       f.extractall("c://",pwd=b"root") ##将文件解压到指定目录,解压密码为root
  except Exception as e:
       print("异常对象的类型是:%s"%type(e))
       print("异常对象的内容是:%s"%e)
  finally:
       f.close()

  9. Python爬取网络数据
  python爬虫应该是最受欢迎的功能,也是广大Python爱好者们入坑的主要的原因。
  Python中有非常多的包支持爬虫,而爬虫包又分为抓取、解析两种。
  比如说requests、urllib这种是网络数据请求工具,也就是抓取包;xpath、re、bs4这种会对抓取下来的网页内容进行解析,称为解析包。
  爬取百度首页图片,并保存到本地:
  # 导入urlopen
  from urllib.request import urlopen
  # 导入BeautifulSoup
  from bs4 import BeautifulSoup as bf
  # 导入urlretrieve函数,用于下载图片
  from urllib.request import urlretrieve
  # 请求获取HTML
  html = urlopen("http://www.baidu.com/")
  # 用BeautifulSoup解析html
  obj = bf(html.read(),'html.parser')
  # 从标签head、title里提取标题
  title = obj.head.title
  # 只提取logo图片的信息
  logo_pic_info = obj.find_all('img',class_="index-logo-src")
  # 提取logo图片的链接
  logo_url = "https:"+logo_pic_info[0]['src']
  # 使用urlretrieve下载图片
  urlretrieve(logo_url, 'logo.png')

  10. Python处理图片图表
  图片处理、图表可视化涉及到图像处理,这也是Python的强项,现在诸如图像识别、计算机视觉等前沿领域也都会用到Python。
  在Python中处理图像的包有scikit Image、PIL、OpenCV等,处理图表的包有matplotlib、plotly、seaborn等。
  对图片进行黑白化处理:
  from PIL import Image
  from PIL import ImageEnhance
  img_main = Image.open(u'E:/login1.png')
  img_main = img_main.convert('L')
  threshold1 = 138
  table1 = []
  for i in range(256):
    if i < threshold1:
      table1.append(0)
    else:
      table1.append(1)
  img_main = img_main.point(table1, "1")
  img_main.save(u'E:/login3.png')

  生成统计图表:
  import numpy as np
  import matplotlib.pyplot as plt
  N = 5
  menMeans = (20, 35, 30, 35, 27)
  womenMeans = (25, 32, 34, 20, 25)
  menStd = (2, 3, 4, 1, 2)
  womenStd = (3, 5, 2, 3, 3)
  ind = np.arange(N)    # the x locations for the groups
  width = 0.35       # the width of the bars: can also be len(x) sequence
  p1 = plt.bar(ind, menMeans, width, yerr=menStd)
  p2 = plt.bar(ind, womenMeans, width,
               bottom=menMeans, yerr=womenStd)
  plt.ylabel('Scores')
  plt.title('Scores by group and gender')
  plt.xticks(ind, ('G1', 'G2', 'G3', 'G4', 'G5'))
  plt.yticks(np.arange(0, 81, 10))
  plt.legend((p1[0], p2[0]), ('Men', 'Women'))
  plt.show()


TAG: 软件开发 Python

 

评分:0

我来说两句

Open Toolbar