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. 《Prism 5.0源码走读》UnityBootstrapper

    UnityBootstrapper (abstract class)继承自Bootstrapper(abstract)类, 在Prism.UnityExtensions.Desktop project ...

  2. PHP 学习笔记 01

    例子: 为什么要学PHP 主观原因: 前段时间在学校处理了毕业的一些事情,回到上海后开始了找工作的旅程.意向工作是WPF开发或者ASP.NET 作为后端的WEB开发. 陆陆续续一直在面试,其中有一家公 ...

  3. c语言中通过指针将数值赋值到制定内存地址

    1.一种直观的方法 假设现在需要往内存0x12ff7c地址上存入一个整型数0x100.我们怎么才能做到呢? 我们知道可以通过一个指针向其指向的内存地址写入数据,那么这里的内存地址0x12ff7c其本质 ...

  4. 解压vmlinuz和解压initrd(initramfs)

    有时就算只得到一个Linux kernel的rpm包或者直接是编译后的vmlinuz和initrd的binary文件,也需要了解其中的一些细节,可能需要去查找这些binary有没有将我想要的patch ...

  5. IIS8 web.config 重定向之后 报错 500.19

    原因是没有安装 URL Rewrite 官方下载地址:http://www.iis.net/downloads/microsoft/url-rewrite#additionalDownloads

  6. 50.ISE布局布线错误

    ERROR:Pack:1654 - The timing-driven placement phase encountered an error. 原因:时钟输出引脚直接接在I/O上了: 方法:在时钟 ...

  7. PBOC规范下的java卡介绍

    JAVA卡与智能卡 什么是 JAVA 卡呢?JAVA 卡是一种可以运行 JAVA 程序的接触式微处理器智能卡.1996 年 11 月,JAVA 卡 1.0 版本的规范正式发布了.如今 JAVA 卡最新 ...

  8. N!大整数阶乘问题

    问题:求N!阶乘,1<=N<10000 思路:windows下面visual 6.0中c一个整型占4个字节(自己可以try一下,printf("%d", sizeof( ...

  9. NSString+NSStringForJava.m

    // // NSString+NSStringForJava.m // NSStringCategory // // Created by Ryan Tang on 12-10-17. // Copy ...

  10. Linux下各硬件装置的文件名

    Linux对计算机各组件/装置的分辨,是和Windows系统完全不一样的!因为各个组件/设备在Linux上都是一个文件!因此,我们在认识各项设备时一定要学习Linux的设备文件名. 众所周知,Linu ...