ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

常用办公脚本工具

2026/8/26 13:33:22 拓冰建站 浏览量
常用办公脚本工具 1 前言本文基于 Android 自动化测试项目、adb常用命令总结整理了一些常用办公脚本后续会根据工作需求持续更新。脚本资源见→常用办公脚本工具脚本目录如下base基础工具包apply具体应用场景注意对于 apply 目录下所有文件的文件名必须能在 phone.py 中找到同名的方法__init__.py 是一个空的文件用于解决 ModuleNotFoundError 报错问题。用户在安装 python 解释器并配置好环境变量后只需单击apply 里面的 bat 文件即可获取需要的信息如清除当前打开 app 的缓存数据、获取当前打开 app 的安装包、获取当前打开 app 的 paclage 和 activity、截屏、截虚拟屏、获取最近截屏或录屏文件等。2 脚本2.1 基础脚本phone.pyimport subprocess as sp import re import os # 手机类封装了对手机的操作 class Phone: # 初始化connect连接类型 def __init__(self, connect_typeusb): self.get_device_id(connect_type) self.model self.get_phone_model() self.size self.get_phone_size() self.vs_display_id self.get_vs_display_id() global bat_path bat_path os.path.abspath(os.path.abspath(os.path.dirname(__file__)) \\..\\apply) \\ # 获取设备 idconnect_type连接类型 def get_device_id(self, connect_typeusb): if connect_type usb: self.id self.get_device_id_by_usb() # 通过 usb 连接设备 elif connect_type wifi: self.ip self.get_device_id_by_wifi() # 通过 wifi 连接设备 # 通过 usb 连接获取设备id程序运行期间需要保持有线连接 def get_device_id_by_usb(self): rst self.check_output(adb devices) id rst.split(\r\n)[1].split()[0] print(id:, id) return id # 通过 wifi 连接获取设备id程序启动时需要有线连接运行后可以断开数据线 def get_device_id_by_wifi(self): rst self.check_output(adb shell \ip addr | grep global\) ip re.findall(\d.\d.\d.\d, rst)[0] return ip # 获取手机型号 def get_phone_model(self): rst self.check_output(adb shell getprop ro.product.model) model re.findall(\w-?\w, rst)[0] print(model:, model) return model # 获取手机分辨率 def get_phone_size(self): rst self.check_output(adb shell wm size) str_size re.findall(\d, rst) x eval(str_size[0]) y eval(str_size[1]) size (x, y) print(size:, size) return size # 获取 visual display id def get_vs_display_id(self): rst self.check_output(adb shell dumpsys activity activities) displays re.findall(Display #\d, rst) vs_id -1 for e in displays: id eval(re.findall(\d, e)[0]) if id 0: vs_id id break return vs_id # 1.获取当前应用的 package 和 Activity def get_pkg_activity(self): rst self.check_output(adb shell dumpsys window | grep mCurrentFocus) win re.findall(\{.\}, rst)[0] items win.replace(}, ).strip().split( ) activity items[-2] for item in reversed(items): if len(item) 10 and / in item: activity item break; pkg activity.split(/)[0] print(package:, pkg) print(activity:, activity) return [pkg, activity] # 2.截手机屏 def screenshot(self): src_path str(self.get_root_path()) /1.png des_path bat_path phone.png os.system(adb shell screencap -p src_path) os.system(adb pull src_path des_path) print(--------------------------------------------) self.screenshot_vs() # 3.获取最近一次截屏或录屏文件 def get_last_shot(self): path1 str(self.get_root_path()) /Pictures/Screenshots/ file1 self.get_last_file(path1) path2 str(self.get_root_path()) /Movies/Screenrecords/ file2 self.get_last_file(path2) path if len(file1) 0: path file1[1] if len(file2) 0 and (len(path) 0 or file2[0] file1[0]): path file2[1] if len(path) 0: print(path:, path) os.system(adb pull path) # 4.清除当前应用的缓存数据 def clear_app_data(self): pkg, activity self.get_pkg_activity() os.system(adb shell pm clear pkg) # 5.获取当前应用的 apk def get_apk(self): path self.get_install_path() os.system(adb pull path bat_path) # 6.获取当前应用的版本号 def get_version(self): pkg, activity self.get_pkg_activity() command adb shell pm dump pkg | grep versionName rst self.check_output(command) version rst.split()[1] print(version:, version) # 截虚拟屏 def screenshot_vs(self): print(vs_display_id:, str(self.vs_display_id)) if self.vs_display_id 0: src_path str(self.get_root_path()) /1_vs.png dest_path bat_path vs_ str(self.vs_display_id) .png os.system(adb shell screencap -d str(self.vs_display_id) -p src_path) os.system(adb pull src_path dest_path) else: print(Visual display does not exist) # 获取当前应用的安装路径 def get_install_path(self): pkg, activity self.get_pkg_activity() rst self.check_output(adb shell pm path pkg) path rst.split(:)[1].split(\\)[0] print(path:, path) return path # 获取最近一次截屏或录屏文件的时间和路径 def get_last_file(self, path): files_list self.get_files_list(path) files [] for file in files_list: temp file.split( ) if temp[-1] . or temp[-1] .. or temp[0][0] d: continue files [temp[-4] _ temp[-3], temp[-1]] if len(files) 0: print(there is no file in path) return [] last_time files[0] last_file files[1] for i in range(2, len(files), 2): if files[i] last_time: last_time files[i] last_file files[i1] print(last_time: last_time , last_file: last_file) return [last_time, path last_file] # 获取存储根路径 def get_root_path(self): rst self.check_output(adb shell ls sdcard) if len(rst) 0: return /sdcard else: print(sdcard不存在或不是目录, 已切换根目录为: /storage) os.system(adb root adb remount) return /storage # 获取路径中文件列表 def get_files_list(self, path): rst self.check_output(adb shell ls -all path) if len(rst) 0: files_list rst.split(\r\n)[1:] if files_list and files_list[-1].strip() : files_list files_list[:-1] return files_list else: return [] # 执行adb命令, 并将结果转换成字符串 def check_output(self, command): try: rst sp.check_output(command) return rst.decode(utf-8).strip() except sp.CalledProcessError: return 说明Phone 类中封装了对手机的操作如获取安装包、截屏、清应用数据、获取最近一次截屏或录屏文件等。invoke.py# 将项目根目录添加到系统路径中防止在命令行运行此脚本时报错 # ModuleNotFoundError: No module named py_script import os import sys root_path screen_shot_path os.path.abspath(os.path.abspath(os.path.dirname(__file__)) \\..) sys.path.append(root_path) # 通过反射调用 Phone 的方法 from base.phone import Phone ph Phone() if hasattr(ph, sys.argv[1]): fun getattr(ph, sys.argv[1]) fun() else: print(This method does not exist in Phoen , sys.argv[1])说明sys.argv指从命令行中获取的输入参数如下sys.argv[1] 指 get_apk。python invoke.py get_apktransit.batecho off python ../base/invoke.py %1 pause说明transit.bat 用于中转业务脚本的文件名并回调 invoke.py“%1” 指 invoke.bat 文件后跟随的第一个参数如在命令行输入如下内容%1 指 get_pkg_activity。transit.bat get_pkg_activity2.2 应用1获取 package 和 activityget_pkg_activity.bat../base/transit.bat %~n0说明“%~n0” 指当前文件名即“get_pkg_activity”。2截手机屏screenshot.bat../base/transit.bat %~n03获取最近截图或录屏get_last_shot.bat../base/transit.bat %~n04清除应用缓存clear_app_data.bat../base/transit.bat %~n05获取 apkget_apk.bat../base/transit.bat %~n06获取应用版本号get_version.bat../base/transit.bat %~n0