python自动压图贴图到Excel小工具!
01
—
需求
1、运行每条测试case成功与否都需要把截图放在img文件夹里;
2、把 (平台)_img 文件夹中的图片压缩写到small_img文件夹里;
3、根据图片命名规则,顺序写入所属case number对应行,顺序写入每条case所有截图;
4、根据平台来贴到excel最合适的位置;
5、最重要一点,是给媳妇写的,提升工作效率;
02
—
环境
windows10/mac + python3.6
python第三方库 xlsxwriter、PIL、argparse
03
—
文件树
04
—
Code
#!/usr/bin/env python3# encoding: utf-8"""@author: Medivh [email protected]: [email protected]: 2017/12/14 18:45"""import xlsxwriterimport datetimeimport osimport loggingLOGGER = logging.getLogger(__name__)def write_img_for_xls(file_name="test.xlsx", img_dir_name="img", sheet_name="案例截图", set_img_row=210.75, set_img_column=23.38, x_scale=0.14, y_scale=0.14, ): """ 读取同目录img文件夹下的所有图片,格式支持png\jpg\bmp。 图片必须遵从 1-1.png、1-2.png、2-1.png、2-2.png格式。 注意:是将图片写入新的excel文件,如果老的excel中有数据,将会替换所有数据。 file_name: 要写入的xlsx文件名 img_dir_name: 图片存放的目录,必须与脚本在同一目录下 set_img_row:设置行高 set_img_column:设置列宽 x_scale:图片宽度缩放比例 y_scale:图片高度缩放比例 :return: Nothing """ start_time = datetime.datetime.now() xls_path = os.path.join(os.getcwd(), file_name) if not os.path.isfile(xls_path): raise MyException("what?你居然不把{}文件跟脚本放在同一个目录下!".format(file_name)) img_path = os.path.join(os.getcwd(), img_dir_name) if not os.path.isdir(img_path): raise MyException("what?你都没有{}文件夹,我咋给你贴图啊~".format(img_dir_name)) all_img = os.listdir(img_path) if not all_img: raise MyException("忽悠我呢?{}文件夹下啥都没有~".format(img_dir_name)) w_book = xlsxwriter.Workbook(xls_path) img_sheet = w_book.add_worksheet(name=sheet_name) count_num = 0 try: for unit_img in all_img: try: img_num = unit_img.split("-") row = int(img_num[0]) column = int(img_num[1].split(".")[0]) suffix = (unit_img.split(".")[1]) if column == 0: LOGGER.warning("图片名称格式有误直接略过!错误文件名:{},“-”前后数字必须从1开始!".format(unit_img)) continue except ValueError: LOGGER.warning("[-]图片命名规则有误直接略过!错误文件名是:{}".format(unit_img)) continue LOGGER.info(">> 正在贴图到第{}条用例,第{}列...".format(row, column)) img_sheet.set_column(firstcol=0, lastcol=0, width=8.38) img_sheet.write(row - 1, 0, "案例{}".format(row)) small_img = os.path.join(os.getcwd(), "{}/{}-{}.{}".format(img_dir_name, row, column, suffix)) img_sheet.set_column(firstcol=column, lastcol=column, width=set_img_column) img_sheet.set_row(row=row - 1, height=set_img_row) img_config = { x_scale: x_scale, y_scale: y_scale } result = img_sheet.insert_image(row - 1, column, small_img, img_config) img_sheet.write_url(row - 1, column + 1, url="https://my.oschina.net/medivhxu/blog/1590012") if not result: LOGGER.info("[+] 写入成功!") count_num += 1 else: LOGGER.error("[-] 写入失败!") except Exception as e: raise MyException("受到不明外力袭击,程序...挂了....\n{}".format(e)) finally: try: w_book.close() except PermissionError: raise MyException("你开着{}文件我让我咋写。。。赶紧关了!".format(file_name)) LOGGER.info("--------------贴图完成--------------") LOGGER.info("程序贴图数量:{},贴图成功数量:{},贴图失败数量:{}".format(len(all_img), count_num, len(all_img) - count_num))class MyException(Exception): passwrite_img_for_xls()
05
—
RUN
06
—
成果
07
—
价值
手动贴图需要半小时?1小时?贴错了?不,这些我都不要,仅需不到5秒,全部搞定,然后就可以干点想干的事~
08
—
性能分析
其实贴图并不需要4秒+,因为xlsxwriter这个模块是自动创建cell的,但是这不是主要原因,主要原因是因为图片太大了,所以保存时间会随着图片加载到内存而线性增长(图片较大或过多,容易导致脚本直接崩溃),优化方式是选用openpyxl模块和最中要的图片预处理。
09
—
PIL图片压缩
#!/usr/bin/env python3# encoding: utf-8"""@author: Medivh [email protected]: [email protected]: 2017/12/14 18:45"""import osfrom PIL import Imagedef compression_img(read_dir_name="img", save_img_file=True, height_shrink_multiple=2, width_shrink_multiple=2): ''' 自动压缩指定文件夹下的所有图片 :param read_dir_name: 指定读取图片的文件夹名称,必须在当前目录下 :param save_img_file: 是否保存压缩后的图片 :param height_shrink_multiple: 设置图片压缩高度的倍数 :param width_shrink_multiple: 设置图片压缩宽度的倍数 :return: nothing ''' img_path = os.path.join(os.getcwd(), read_dir_name) all_img = os.listdir(img_path) for unit_img in all_img: try: img_num = unit_img.split("-") row = int(img_num[0]) column = int(img_num[1].split(".")[0]) suffix = (unit_img.split(".")[1]) if column == 0: print("图片名称格式有误直接略过!错误文件名:{},“-”前后数字必须从1开始!".format(unit_img)) continue except ValueError: print("[-]图片命名规则有误直接略过!请参考1-1.png格式从新运行或手动解决!") print("错误文件名是:{}".format(unit_img)) continue img_fp = os.path.join(img_path, unit_img) origin_img = Image.open(img_fp) w, h = origin_img.size small_img = origin_img.resize((int(w / height_shrink_multiple), int(h / width_shrink_multiple))) if save_img_file: img_save_fp = os.path.join(os.getcwd(), "small_img") if os.path.isdir(os.path.join(os.getcwd(), "small_img")): print("warning, 已有small_img文件夹!直接保存到里面了!") small_img.save(os.path.join(img_save_fp, ("{}-{}.{}".format(row, column, suffix)))) else: os.mkdir("small_img") print("新建文件夹“small_img”,压缩后的图片将存储在该文件夹中。") small_img.save(os.path.join(img_save_fp, ("{}-{}.{}".format(row, column, suffix)))) print(">> 正在处理图像{}-{}.{},原像素高和宽{},压缩后的高和宽{}".format(row, column, suffix, (w, h), small_img.size)) small_img.close() print("--------------图片压缩完成--------------")compression_img()
windows command和linux/mac terminal下运行效果:
The end~
源码获取私信小编01