import os import fitz import requests #剪切pdf为图片 def CutPdf(pdfPath,savePath): try: doc = fitz.open(pdfPath) #pdf路径 for pg in range(doc.pageCount): if pg >= 1: break; page = doc[pg] rotate = int(0) # 每个尺寸的缩放系数为2,这将为我们生成分辨率提高四倍的图像。 zoom_x = 2.0 zoom_y = 2.0 trans = fitz.Matrix(zoom_x, zoom_y).preRotate(rotate) pm = page.getPixmap(matrix=trans, alpha=False) pm.writePNG(savePath+'%s.png'% str(pg+1) ) #最终存储路径 except Exception as e: print('CutPdf出现异常:'+str(e)) def main(): pdfUrl = "cards" fileList = os.listdir(pdfUrl) index=1 for item in fileList: url= pdfUrl + "/" + item url2= "images/" + item.split('.')[0] CutPdf(url,url2) print("完成裁剪:"+str(index)) index+=1 if __name__ == "__main__": main()
文章评论