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("")

  

最新文章

  1. Android OpenCV 图像识别
  2. 关于mysql的错误 - no query specified
  3. Android Studio调试功能使用总结
  4. python爬取糗百第一页的笑话
  5. linux安装rz和sz
  6. 步步学LINQ to SQL:使用LINQ检索数据【转】
  7. 【概率DP入门】
  8. php 垃圾回收机制----写时复制和引用计数
  9. Keil 中的预处理命令const
  10. PyConChina2016 北京站 献给Python开发者
  11. Qt编译Oracle OCI驱动
  12. 大三小学期 Android开发的一些经验
  13. adb shell am broadcast 手动发送广播及adb shell am/pm其他命令
  14. 关于在最新的 Visual Studio 2017 版本中使用 Web Deploy 遇到的 SSL 连接错误
  15. JNI详解---从不懂到理解
  16. Python入门 值内存管理与所有的关键字
  17. 计算字符串最后一个单词的长度,单词以空格隔开。 java算法
  18. http to https
  19. haoi2006_受欢迎的牛_Solution
  20. yearProgress.vue

热门文章

  1. oracle 11g 表空间使用率
  2. RMAN - 备份异机恢复
  3. 随机数范围扩展(如rand7()到rand10())(转)
  4. spring使用JdbcDaoSupport中封装的JdbcTemplate进行query
  5. 使用ViewSwitcher和ViewFlipper在不同布局中切换
  6. python 实现求和、计数、最大最小值、平均值、中位数、标准偏差、百分比。
  7. [无人值守安装操作系统]__RHEL6__FTP+TFTP+DHCP+Kickstart+PXE
  8. linux free 命令
  9. 小组开发项目针对性的NABC分析
  10. TWaver初学实战——基于HTML5的交互式地铁图