Skip to content

常用包

https://github.com/vinta/awesome-python

日期时间

https://github.com/arrow-py/arrow

https://arrow.readthedocs.io/en/latest/

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
In [17]: date = arrow.get(2021, 2, 28)
In [20]: date.shift(years=-1)
Out[20]: <Arrow [2020-02-28T00:00:00+00:00]>

In [22]: date = arrow.get(2020, 2, 29)

In [23]: date.shift(years=-1)
Out[23]: <Arrow [2019-02-28T00:00:00+00:00]>

In [24]: date = arrow.get(2020, 3, 31)

In [25]: date.shift(months=-1)
Out[25]: <Arrow [2020-02-29T00:00:00+00:00]>

In [26]: date.shift(months=-2)
Out[26]: <Arrow [2020-01-31T00:00:00+00:00]>

In [27]: date.shift(months=-5)
Out[27]: <Arrow [2019-10-31T00:00:00+00:00]>

In [28]: date.shift(months=-6)
Out[28]: <Arrow [2019-09-30T00:00:00+00:00]>
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
local = utc.to('US/Pacific')
local
<Arrow [2013-05-11T13:23:58.970460-07:00]>

local.timestamp()
1368303838.970460

local.format()
'2013-05-11 13:23:58 -07:00'

local.format('YYYY-MM-DD HH:mm:ss ZZ')
'2013-05-11 13:23:58 -07:00'

pytz:
https://launchpad.net/pytz

构建命令行工具

typer

https://github.com/tiangolo/typer

https://typer.tiangolo.com/

进度条

tqdm:

https://github.com/tqdm/tqdm

https://tqdm.github.io/

1
2
3
4
5
6
from time import sleep
from tqdm import tqdm

for i in tqdm(range(1, 500)):
   sleep(0.01)
sleep(0.5)

表格形式打印

https://github.com/astanin/python-tabulate

https://github.com/jazzband/prettytable

pip install prettytable

1
2
3
4
5
from prettytable import PrettyTable
x = PrettyTable()
x.field_names = ["City name", "Area", "Population", "Annual Rainfall"]
x.add_row(["Adelaide", 1295, 1158259, 600.5])
print(x.get_string())

其他

PIL

PIL 模块中包含着各种用于处理图像的工具类,其中比较常见的工具类如表所示

工具类 描述
Image 用于加载图像以及创建新的图像等
ImageChops 用于各种特效效果、图像合成、算法绘画等
ImageColor 该类包含颜色表和从 CSS3 样式颜色说明符到 RGB 元祖的转换器
ImageDraw 该类为 Image 对象提供简单的 2D 图形。您可以使用此模块创建新图像,对现有图像进行注释或修饰,以及即时生成图形供 Web 使用

Pillow

https://github.com/python-pillow/Pillow

pyautogui

https://github.com/asweigart/pyautogui

https://pyautogui.readthedocs.io/en/latest/

根据截图在屏幕上定位:

https://pyautogui.readthedocs.io/en/latest/screenshot.html#the-locate-functions

需要安装 open-cv

mac 下直接pip install opencv-python

windows 下安装可能会有很多问题

示例:

找到目标按钮并点击
但是 opencv 识别出的坐标是实际坐标的 2 倍,还不知道什么原因。。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
import pyautogui
from pyautogui import locateOnScreen
import time
import cv2
time.sleep(5)

screenWidth, screenHeight = pyautogui.size()
print(screenWidth, screenHeight)
currentMouseX, currentMouseY = pyautogui.position()
print(currentMouseX, currentMouseY)
# 可选confidence关键字参数指定函数在屏幕上定位图像的准确性。如果函数由于可忽略的像素差异而无法定位图像,这将非常有用:
location1 = locateOnScreen('test.jpg', confidence=0.9)
print(location1)
button7point = pyautogui.center(location1)
x, y = button7point
x //= 2
y //= 2
print(button7point)
pyautogui.moveTo(x, y)
pyautogui.click()
pyautogui.click()