赞
踩
因为内网,不能发送大于10M的文件,无奈只能将pdf切分进行
import fitz import os def covert2pic(zoom): if os.path.exists('.pdf'): # 临时文件,需为空 os.removedirs('.pdf') os.mkdir('.pdf') for pg in range(totaling): page = doc[pg] zoom = int(zoom) #值越大,分辨率越高,文件越清晰 rotate = int(0) print(page) trans = fitz.Matrix(zoom / 100.0, zoom / 100.0).preRotate(rotate) pm = page.getPixmap(matrix=trans, alpha=False) lurl='.pdf/%s.jpg' % str(pg+1) pm.writePNG(lurl) doc.close() def pic2pdf(obj): doc = fitz.open() for pg in range(totaling): img = '.pdf/%s.jpg' % str(pg+1) imgdoc = fitz.open(img) # 打开图片 pdfbytes = imgdoc.convertToPDF() # 使用图片创建单页的 PDF os.remove(img) imgpdf = fitz.open("pdf", pdfbytes) doc.insertPDF(imgpdf) # 将当前页插入文档 if os.path.exists(obj): # 若文件存在先删除 os.remove(obj) doc.save(obj) # 保存pdf文件 doc.close() def pdfz(sor, obj, zoom): covert2pic(zoom) pic2pdf(obj) if __name__ == "__main__": sor = "企业绩效评价标准值2021.pdf" # 需要压缩的PDF文件 obj = "new_2" + sor doc = fitz.open(sor) totaling = doc.pageCount zoom = 30 # 清晰度调节,缩放比率 pdfz(sor, obj, zoom) os.removedirs('.pdf')
from PyPDF2 import PdfFileWriter, PdfFileReader
inputpdf = PdfFileReader(open("企业绩效评价标准值2021.pdf", "rb"))
yeshu = 30 # 请修改这个参数!!! 30页为一个文件
for i in range(inputpdf.numPages//yeshu+1):
output = PdfFileWriter()
for j in range(i*yeshu,(i+1)*yeshu):
if j <420: # 控制整个pdf页数,对应修改
output.addPage(inputpdf.getPage(j))
with open("./output/企业绩效评价标准值2021-page%s.pdf" % str(j-yeshu+2), "wb") as outputStream:
output.write(outputStream)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。