robotframework  这个需要了解的请度娘。本文实现的是一个小功能。大体分为如下几个步骤

1)给定一个pdf文件。

2)读取pdf文件内容,并解析为文本内容。

3)通过给定的内容,比对pdf文件内容。

4)输出测试结果。

5)发送结果到指定邮件。

其中读取pdf文件内容,使用的是pdfminer

其他的就是自己包装。

涉及到部分隐私内容,部分代码如下:

# -*- coding: UTF-8 -*-
# coding=utf-8
#from __future__ import unicode_literals
import sys
import os import subprocess
import time
import re
from robot.libraries.BuiltIn import BuiltIn
from mailcommon import mailcommon
from pdfminer.pdfdocument import PDFDocument
from pdfminer.pdfparser import PDFParser
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.pdfdevice import PDFDevice, TagExtractor
from pdfminer.pdfpage import PDFPage
from pdfminer.converter import XMLConverter, HTMLConverter, TextConverter
from pdfminer.cmapdb import CMapDB
from pdfminer.layout import LAParams
from pdfminer.image import ImageWriter
import cStringIO
#reload(sys)
#sys.setdefaultencoding('utf-8') class pdfContentKeywords(object):
ROBOT_LIBRARY_SCOPE = 'Global' root = os.path.abspath(os.path.join(__file__, '..')) def __init__(self):
self.builtin = BuiltIn() def handlepdf(self, path, param_dict,email, fail=True):
''' Reads a specified directory pdf file and diff ... `path`: pdf file path `param_dict`: Require comparison data `fail`: If there are differences it will throw an exception and test will fail
defaults to True, if False test's will continue '''
param_dict=eval(param_dict)
self.builtin.log("pdf: %s" %path)
self.builtin.log("param: %s" %param_dict) fail = self.builtin.convert_to_boolean(fail)
result=dict
if fail:
result=self._diffContent(path, param_dict)
else:
try:
result=self._diffContent(path, param_dict)
except Exception, e:
self.builtin.log(e)
msg=''
for i in result:
print u"result:%s=" % i,result[i]
#print self.builtin.log(i)
msg+="=>".join([i,str("检验通过" if result[i]==1 else "检验没通过")])
mail_obj=mailcommon()
print u"mail MSG:%s" % msg
maillist=[]
maillist.append(email)
mail_obj.send_mail(maillist,'测试结果分析',msg);
self.builtin.log("End") def _readPdf(self,path):
caching = False
password = ''
pagenos = set()
maxpages = 0
# output option
outfile = None
outtype = None
imagewriter = None
rotation = 0
layoutmode = 'normal'
codec = 'utf-8'
pageno = 1
scale = 1
showpageno = True
laparams = LAParams()
content=''
outfp=cStringIO.StringIO()
try:
rsrcmgr = PDFResourceManager(caching=caching) device = TextConverter(rsrcmgr, outfp, codec=codec, laparams=laparams,
imagewriter=imagewriter)
fp = file(path, 'rb')
interpreter = PDFPageInterpreter(rsrcmgr, device)
for page in PDFPage.get_pages(fp, pagenos,
maxpages=maxpages, password=password,
caching=caching, check_extractable=True):
page.rotate = (page.rotate+rotation) % 360
interpreter.process_page(page)
fp.close()
device.close()
content= outfp.getvalue()
outfp.close()
except Exception, e:
print "Exception:%s",e
self.builtin.log(e)
content=content.replace('\n','').replace('\t','').strip('\n')
print u"pdf file content:%s"% content#.encode('utf-8').strip()
return content def _diffContent(self,path,p_dict):
result=dict()
try:
pdfContent=self._readPdf(path)
fail=0
for i in p_dict:
print "dict[%s]=" % i,p_dict[i]
if re.search(r''+i+p_dict[i] ,pdfContent,re.I | re.M|re.X):
fail=1
result[i+p_dict[i]]=fail
except Exception, e:
print "Exception:%s",e
self.builtin.log(e)
return result if __name__ == "__main__":
d = pdfContentKeywords()
print d.handlepdf("C:\\Print.pdf",{"船名/航次:":"CMACGMFIDELIO/B14WESVLC/VALENCIA"},'roger_he@5uzh.com',False)
#d._readPdf("")

  

robotframework 测试工具添加PDF文件内容匹配插件的更多相关文章

  1. 如何修改PDF文件内容,PDF怎么添加背景

    很多的情况下,大家都会遇到PDF文件,不管是在学习中还是在工作中,对于PDF文件,文件的修改编辑是需要用到PDF编辑软件的,在编辑文件的时候,发现文件的页面是有背景颜色的,又该如何修改背景颜色呢,不会 ...

  2. 深入学习Python解析并解密PDF文件内容的方法

    前面学习了解析PDF文档,并写入文档的知识,那篇文章的名字为深入学习Python解析并读取PDF文件内容的方法. 链接如下:https://www.cnblogs.com/wj-1314/p/9429 ...

  3. 怎么编辑PDF文件内容,PDF文件编辑方法

    怎样编辑PDF文件内容?这是一个常常困扰我们的问题,工作当中我们经常会收到PDF格式的文件,但有时的文件内容不是我们想要的或者是觉得不合理的需要改掉.但是每次有这样的问题时都没有什么好的解决方法,每次 ...

  4. 编辑方法分享之如何编辑PDF文件内容

    我们现在在工作中会经常使用到PDF文件,还会有遇到需要编辑PDF文件的时候,PDF文件的编辑问题一直是个大难题.很多朋友在面对PDF文件的时候束手无策,不知道该怎么对它进行编辑.下面小编就教给大家一个 ...

  5. 深入学习python解析并读取PDF文件内容的方法

    这篇文章主要学习了python解析并读取PDF文件内容的方法,包括对学习库的应用,python2.7和python3.6中python解析PDF文件内容库的更新,包括对pdfminer库的详细解释和应 ...

  6. pdf文件内容查看器 -- 采用wpf开发

    前言 pdf是一种应用非常广的版式文档格式,已成为事实上的国际标准.关于pdf格式的文章汗牛充栋,本文也是关于pdf格式的文章,但是本文不是纸上谈兵:本人这几周一直研究pdf格式内容,不但对pfd格式 ...

  7. javaWeb项目springMVC框架下利用ITextpdf 工具打印PDF文件的方法(打印表单、插入图片)

    方法一:打印PDF表单以及在PDF中加入图片 需要的资料: jar包:iTextAsian.jar ,itext-2.1.7.jar: 源码: public static void main(Stri ...

  8. 工具类_JavaPOI_Office文件内容读取

    文件内容读取工具类,亲测可用 maven依赖: <dependency> <groupId>org.apache.poi</groupId> <artifac ...

  9. js直接打印pdf文件内容

    (1)需求:是网页上打开一个pdf文件,然后直接打开打印机,就是直接打印,不用用户再次点击打印按钮,这样用户体验好 (2)经历: 我在网上找了资料就是使用window.print(),但是这个只是打印 ...

随机推荐

  1. 'mysql.column_stats' doesn't exist and Table 'mysql.index_stats' doesn't exist

    在生产库MariabDB中修改字段类型,提示如下错误:​Table 'mysql.column_stats' doesn't existTable 'mysql.index_stats' doesn' ...

  2. 启动FM预算基金管理模块后,0L总账消失的解决办法

    只要用SE38运行一下:FMGL_CHANGE_APPL_IN_LEDGER 问题就解决了.

  3. poj 1459 Power Network

    题目连接 http://poj.org/problem?id=1459 Power Network Description A power network consists of nodes (pow ...

  4. sharepoint 2010 误删除AD组用户不能访问

    不小心误操作把ad中的组删除了,在sharepoint中是通过组给的权限,在ad中新建了一个同样名的组给了权限组下面的用户还是不能访问. 解决方法: 在sharepoint中把这组从网站集中删除,重新 ...

  5. 主要从架构上来做优化,负载均衡、CDN、静态化、数据库的水平切割和纵向切割、读写分离、分布式缓存着手

    语言知识一种工具,甚至技术本身也只是一种工具,本身并不值钱,关键在于用于何种行业,产生了什么价值. 但从语言来看,我个人更喜欢php,然后是C#,然后是java从框架而言,先是java,然后C#,再次 ...

  6. 60.ISE PhysDesignRules ERROR

    PhysDesignRules:2100 - Issue with pin connections and/or configuration on block:<U_ila_pro_0/U0/I ...

  7. 51nod 1109 01组成的N的倍数

    用01 组成 N的最小倍数 这个BFS搜索就好. 类似这道:  ZOJ Problem Set - 1530 每次 要么是0 要么是1, 记入余数,和前驱. #include<bits/stdc ...

  8. html标记列表应用

    一.[ul]无序列表 1.无序列表====== 二.[ol]有序列表 1.有序列表用于段落有序的排列, <ol> <li>内容</li> </ol> 三 ...

  9. mongodb修改器

    mongodb修改器 转载自:http://blog.csdn.net/mcpang/article/details/7752736 mongodb修改器(\(inc/\)set/\(unset/\) ...

  10. Hibernate 检索查询的几种方式(HQL,QBC,本地SQL,集成Spring等)

    1.非集成Spring hibernate的检索方式,主要有以下五种. 1.导航对象图检索方式.(根据已经加载的对象,导航到其他对象.) 2.OID检索方式.(按照对象的OID来检索对象.) 3.HQ ...