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. 自己遇到的ajax调用ashx文件无法获取返回值的一种情况

    无法获取返回值的ashx文件大致如下: public void ProcessRequest (HttpContext context) { context.Response.ContentType ...

  2. Percona XtraDB Cluster Strict Mode(PXC 5.7)

    在Percona XtraDB Cluster集群架构中,为了避免多主节点导致的数据异常,或者说一些不被支持的特性引发的数据不一致的情形,PXC集群可以通过配置pxc_strict_mode这个变量来 ...

  3. 华硕R系列的解剖图

    1.键盘底部 2.右侧光驱,右下硬盘 3.电源 4.主板 5. 6.4G内存

  4. Vue 父组件调用子组件函数的方法

    parent.vue(父组件的内容): <template> <div @click="divClick"> <info-wnd ref=" ...

  5. 3.Jmeter参数化

    1.参数化的三种方式 ${变量} 1 用户定义的变量 2 添加配置元件 CSV Data Set Config ,导入.csv文档 3 利用函数助手中的函数获取参数值     主要用 _Random函 ...

  6. SpringBoot 之静态资源

    boot 的默认的静态资源有多个, 由 ResourceProperties 配置了默认值: private static final String[] CLASSPATH_RESOURCE_LOCA ...

  7. 发送Http

    /** * 向指定 URL 发送POST方法的请求 * * @param url * 发送请求的 URL * @param param * 请求参数,请求参数应该是 name1=value1& ...

  8. Android单元测试学习(一)

    什么是单元测试 首先总结一下什么是单元测试,单元测试中的单元在Android或Java中可以理解为某个类中的某一个方法,因此单元测试就是针对Android或Java中某个类中的某一个方法中的逻辑代码进 ...

  9. SpringCloud系列二:Restful 基础架构(搭建项目环境、创建 Dept 微服务、客户端调用微服务)

    1.概念:Restful 基础架构 2.具体内容 对于 Rest 基础架构实现处理是 SpringCloud 核心所在,其基本操作形式在 SpringBoot 之中已经有了明确的讲解,那么本次为 了清 ...

  10. Docekr 挂在卷之后访问目录时异常 cannot open directory '.': Permission denied 的解决办法

    1,原因,原因是CentOS7 中的安全模块 selinux 把权限禁掉了 2,解决办法如下 2.1,运行容器是加参数在 --privileged=true   (个人认为这是最佳方式,推荐使用) 如 ...