How to convert HTML Report to picture format in Email? So that we can see the automation report also at home or on mobile phone anywhere.

We tried to use phantomJs to get the full-page screenshot of HTML, it doesn't work well on some computers, then we found that the newest Chrome doesn't support it anymore, and Chrome has use its Headless mode to replace phantomJs.

Version 1 : phantomJs

# -*- coding: utf-8 -*-
import time
import os from selenium import webdriver
jenkinsJobName=os.getenv("JOB_NAME")
url="http://10.2.3.3/testAgent/AutoAnaylsisReport.html"
print url
save_fn="buildNumResult.PNG"
driver = webdriver.PhantomJS()
driver.maximize_window()
driver.get(url) # Load page
time.sleep(30)
driver.save_screenshot(save_fn)
driver.close()
time.sleep(5)
os.system("taskkill /F /IM phantomjs.exe")

Version 2: Chrome Headless

# -*- coding: utf-8 -*-
import time
import os from selenium import webdriver
from selenium.webdriver.common.keys import Keys url="http://10.2.4.1/testAgent/BillingAnaylisisReport.html"
print url
save_fn="buildNumResult.PNG" option = webdriver.ChromeOptions()
option.add_argument('--headless')
option.add_argument('--disable-gpu')
option.add_argument("--window-size=1280,1024")
option.add_argument("--hide-scrollbars") driver = webdriver.Chrome(chrome_options=option) driver.get(url)
print(driver.title) scroll_width = driver.execute_script('return document.body.parentNode.scrollWidth')
scroll_height = driver.execute_script('return document.body.parentNode.scrollHeight')
driver.set_window_size(scroll_width, scroll_height)
driver.save_screenshot(save_fn)
driver.quit()

Version 3 , 为了把截图放在邮件里直接展示,需要把图片截小一点。然后在邮件内容的HTML中加上一行:${FILE,path="report.html"}

# -*- coding: utf-8 -*-
import time,os
from PIL import Image
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
time.sleep(10)
url="http://10.249.4.17/testRailAgent/BillingAnaylisisReport.html?jenkinsJobName=Billing_Office_Live&projectUATName=Billing_Office_UAT&type=1"
print url
save_fn="buildNumResult.PNG" option = webdriver.ChromeOptions()
option.add_argument('--headless')
option.add_argument('--disable-gpu')
option.add_argument("--window-size=1280,1024")
option.add_argument("--hide-scrollbars") driver = webdriver.Chrome(chrome_options=option) driver.get(url)
time.sleep(30) scroll_width = driver.execute_script('return document.body.parentNode.scrollWidth')
scroll_height = driver.execute_script('return document.body.parentNode.scrollHeight')
driver.set_window_size(scroll_width, scroll_height)
driver.save_screenshot(save_fn)
time.sleep(5)
driver.quit()
im = Image.open(save_fn)
w,h = im.size
imgCount = h/2000+1
size=h/imgCount
left = 0
shang = 0
index = 0
for i in range(imgCount):
i=i+1
shang += 1
a = size * left
b = size *(i-1)
c = w
d = size * i
region = im.crop((a, b, c, d))
region.save("report%s.png" %i) fp = open("report.html","w+b") #打开一个文本文件
for i in range(1,imgCount+1):
fp.write('<img src="report'+str(i)+'.png"></img>') #写入数据
fp.close() #关闭文件

  

[Python] 怎么把HTML的报告转换为图片,利用无头浏览器的更多相关文章

  1. 将PPT文件内容转换为图片放在Email邮件正文中发送

    通过Email推送统计报告.一般除了要求将PPT报告文件作为附件发给用户,同时希望将报告内容在邮件中直观展示. 一份统计报告中经常包含柱状图.饼图.好看的图表,这些信息要直接在Email中展示比较复杂 ...

  2. 20184302 2019-2020-2 《Python程序设计》实验四报告

    20184302 2019-2020-2 <Python程序设计>实验四报告 课程:<Python程序设计> 班级: 1843 姓名: 李新锐 学号:184302 实验教师:王 ...

  3. 20192113 2020-2021-2 《Python程序设计》实验二报告

    20192113 2020-2021-2 <Python程序设计>实验二报告 课程:<Python程序设计> 班级: 1921 姓名: 衣丽莎 学号:20192113 实验教师 ...

  4. 20192113 2020-2021-2 《Python程序设计》实验一报告

    20192113 2020-2021-2 <Python程序设计>实验一报告 课程:<Python程序设计> 班级: 1921 姓名: 衣丽莎 学号:20192113 实验教师 ...

  5. 20192204 2019-2020-2 《Python程序设计》实验四报告

    20192204 2019-2020-2 <Python程序设计>实验四报告 课程:<Python程序设计> 班级: 1922 姓名: 李龙威 学号:20192204 实验教师 ...

  6. 分享:Svg文件转换为图片(调用 Inkscape 命令行)

    其实只是做了简单封装,可以方便进行批量转换. 获取Svg对象坐标的代码请看:根据svg节点对象类型和路径值转换坐标值, DrawingColor方法是进行颜色填充的. /// <summary& ...

  7. MVC把随机产生的字符串转换为图片

    原文:MVC把随机产生的字符串转换为图片 Insus.NET在这篇中<在ASP.NET MVC应用程序中随机获取一个字符串>http://www.cnblogs.com/insus/p/3 ...

  8. 【使用Itext处理PDF文档(新建PDF文件、修改PDF文件、PDF中插入图片、将PDF文件转换为图片)】

    iText简介 iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库.通过iText不仅可以生成PDF或rtf的文档,而且可以将XML.Html文件转 ...

  9. C# CAD批量转换为图片

    最近写了个工具,将指定目录下的CAD文件批量转换为图片格式. 首先需要添加对应的引用 : 在AutoCAD2008的环境下对应AutoCAD 2008 Type Library 和 AutoCAD/O ...

随机推荐

  1. 红米note3发热严重,小米真垃圾!

    红米note3全网通高配版,高通处理器骁龙650(MSM8956),夏天在有空调的房间,上网几分钟手机发烫,真垃圾! ROM已经是官方最新稳定版.MIUI8.5.2.0(LHNCNED) 红米NOTE ...

  2. centos7 下安装Docker CE

    前提条件 操作系统要求 要保证centos-extrasrepository开启(enabled).默认处于开启状态. 推荐使用overlay2存储驱动 卸载老版本 $ sudo yum remove ...

  3. unittest 出报告 并配合 jenkins,发现有用例错误,但是构建没出现红点 的解决方法

    加了个 判断 测试用例总数 和 测试运行成功数 是否一致的判断,不一致 就断言失败,jenkins哪里是红点

  4. iterator简单描述

    Item 26. Prefer iterator to const iterator, reverse_iterator, and const_reverse_iterator. 上面一段话,是< ...

  5. Zabbix监控进程(进程消失后钉钉报警)

    用于python报警的脚本如下:(钉钉机器人的连接需要修改) #!/usr/bin/python3# -*- coding: utf-8 -*-# Author: aiker@gdedu.ml# My ...

  6. python http请求及多线程应用

    目录 概述 tomorrow包准备 运行环境遇到的问题 其他尝试未果 概述 今天, 使用python3 模拟下发包, http get 请求, 然后 采用tomorrow 多线程. 代码如下: # c ...

  7. mysql 使用 GROUP BY 时报错 ERROR 1055 (42000)

    解决方法1:SET sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));这种修改只是暂时性的,还是在 my.ini 文件中的sq ...

  8. 关于PHP代码复用‘traits’的一段代码

    附:代码摘自菜鸟教程 <?php// 定义一个类名Base对象,并带有公共函数sayHello class Base { public function sayHello() { echo 'H ...

  9. Synchronized方法锁、对象锁、类锁区别

    synchronized,这个东西我们一般称之为”同步锁“,他在修饰代码块的时候需要传入一个引用对象作为“锁”的对象. 在修饰方法的时候,默认是当前对象作为锁的对象 在修饰类时,默认是当前类的Clas ...

  10. 转载:oracle 启动过程--oracle深入研究

    Oracle数据库的启动-nomount状态深入解析 通常所说的Oracle Server主要由两个部分组成:Instance和Database.Instance是指一组后台进程(在Windows上是 ...