3. pdf操作
3.2 合并多个pdf为一个pdf
需要用到一个第三方模块:PyPDF2
安装:pip install PyPDF2
例子:
#!/usr/bin/env python import PyPDF2 import os import re import codecs path = 'aminglinux' pdflist = list() for i in os.listdir(path): if i.endswith('.pdf'): pdflist.append(i) #列表排序 dic = dict() for a in pdflist: try: dic[a] = re.search(r'chapter(\d+).*', a).group(1) except: continue newlist = sorted(dic, key=lambda x:int(dic[x]), reverse=False) os.chdir(path) pdfwt = PyPDF2.PdfFileWriter() //创建一个空白的pdf文件 for p in newlist: pdfrd = PyPDF2.PdfFileReader(open(p, 'rb')) //以只读方式打开pdf文件 for page in range(pdfrd.numPages): pdfwt.addPage(pdfrd.getPage(page)) //将打开的pdf文件一页一页地复制到空白的pdf中 with codecs.open('combine.pdf', 'wb') as f: pdfwt.write(f) //把复制的内容全部写入conbine.pdf中
3.3 读取pdf的内容
使用第三方模块:pdfminer
安装:pip install pdfminer
注意:python3装不了这个模块,因为__init__.py里面有一句print代码没有括号,在python3环境下执行会报错
例子:
!/usr/bin/env python #coding:utf8 from pdfminer.pdfparser import PDFParser from pdfminer.pdfdocument import PDFDocument from pdfminer.pdfpage import PDFPage from pdfminer.pdfpage import PDFTextExtractionNotAllowed from pdfminer.pdfinterp import PDFResourceManager from pdfminer.pdfinterp import PDFPageInterpreter from pdfminer.pdfdevice import PDFDevice from pdfminer.layout import LAParams from pdfminer.converter import PDFPageAggregator fp = open('2.pdf', 'rb') parser = PDFParser(fp) //创建一个与文档相关联的解释器 doc = PDFDocument(parser) //pdf文档对象 parser.set_document(doc) //连接解释器和文档对象 resource = PDFResourceManager() //创建pdf资源管理器 laparam = LAParams() //参数分析器 device = PDFPageAggregator(resource, laparams=laparam) //创建一个聚合器 interpreter = PDFPageInterpreter(resource, device) //创建pdf页面解释器 for page in PDFPage.create_pages(doc): //使用文档对象得到页面集合 interpreter.process_page(page) //使用页面解释器来读取 layout=device.get_result() //使用聚合器来获取内容 for out in layout: if hasattr(out, "get_text"): print out.get_text()
4. 图片处理
PIL(Python Imaging Library)是python中最常用的图像处理库
安装:
- python 2.x:http://www.pythonware.com/products/pil/ 下载对应的版本
- python 3.x:PIL模块在python 3.x中替换成pillow模块,直接使用 pip install pillow 即可
导入模块时,不论时python 2.x 还是python 3.x,都是:from PIL import Image
图片对象img有三个属性
- format:识别图像的源格式,如果该文件不是从为念中读取,则format的值为None
- size:返回一个元组,元组的两个元素分别时像素意义上宽和高
- mode:RGB(true color image)、L(luminance)和CMTK(pre-press image)
图片对象img的四个方法:
- show():显示最近加载的图像
- open(filename):打开文件
- save(filename):保存文件
- crop((left, upper, right, lower)):从图像中提取出某个矩形大小的图像,这个方法接收一个四元素的元组作为参数,四元素分别为左上右下,坐标系统的原点(0, 0)是左上角
图片对象img的几何处理:
- img.resize((宽, 高)) //调整图片大小
- img.rotate(45) //逆时针旋转45度
- img.transpose(img.FLIP_LEFT_RIGHT) //左右对换
- img.transponse(img.FLIP_TOP_BOTTOM) //上下对换
- img.transponse(img.ROTATE_90) //旋转90度
- img.transponse(img.ROTATE_180) //旋转180度
- img.transponse(img.ROTATE_270) //旋转270度
例一:
#!/usr/bin/env python from PIL import Image img = Image.open('1.JPG') print (img.format, img.size, img.mode) img.show() ------------------------------------------------> JPEG (3264, 2448) RGB
例二:图片缩放
#!/usr/bin/env python from PIL import Image img = Image.open('1.JPG') x, y = img.size newx = 1920 newy = int((newx * y)/x) newimg = img.resize((newx, newy), Image.ANTIALIAS) newimg.save('2.JPG')
例三:裁剪
#!/usr/bin/env python from PIL import Image img = Image.open('2.JPG') target = (900, 680, 1170, 770) newimg = img.crop(target) newimg.save('cutting3.JPG')
例四:验证码
# !/usr/bin/env python # coding:utf8 import random import string from PIL import Image, ImageDraw, ImageFont, ImageFilter class VeifyCode(object): def __init__(self, fontpath, number, size, bgcolor, fontcolor, linecolor, linenumber): self.drawline = True #默认加入干扰线 self.fontpath = fontpath self.number = number self.size = size self.bgcolor = bgcolor self.fontcolor = fontcolor self.linecolor = linecolor self.linenumber = linenumber # 用来随机生成一个字符串 def getText(self): return ''.join(random.sample(string.ascii_letters + string.digits, self.number)) # 用来绘制干扰线 def drawInterferentLine(self, draw, width, height): begin = (random.randint(0, width), random.randint(0, height)) end = (random.randint(0, width), random.randint(0, height)) draw.line([begin, end], fill=linecolor) # 生成验证码 def getImage(self): width, height = self.size image = Image.new('RGBA', (width, height), bgcolor) # 创建图片 font = ImageFont.truetype(self.fontpath, 25) # 验证码的字体 draw = ImageDraw.Draw(image) # 创建画笔 text = self.getText() # 生成字符串 # print(text) font_width, font_height = font.getsize(text) draw.text(((width - font_width) / number, (height - font_height) / number), text, font=font, fill=fontcolor) # 填充字符串 if self.drawline: for i in range(self.linenumber): self.drawInterferentLine(draw, width, height) # 创建扭曲 # image = image.transform((width + 20, height + 10), Image.AFFINE, (1, -0.3, 0, -0.1, 1, 0), Image.BILINEAR) # 滤镜,边境加强 # image = image.filter(ImageFilter.EDGE_ENHANCE_MORE) image.save('verifycode.png') # 保存验证码图片 if __name__ == "__main__": fontpath = 'C:\Windows\Fonts\Arial.ttf' #字体位置,不同版本的系统可能路径有所不同 number = 4 #生成多少位的验证码 size = (100, 30) #生成验证码图片的高度和宽度 bgcolor = (255, 255, 255) #背景颜色,默认白色 fontcolor = (0, 0, 255) #字体颜色,默认蓝色 linecolor = (255, 0, 0) #干扰线颜色,默认红色 linenumber = 20 #加入干扰线的条数上限 verifyCode = VeifyCode(fontpath, number, size, bgcolor, fontcolor, linecolor, linenumber) verifyCode.getImage()