-- coding: utf-8 --
import os
import fitz
import PyPDF2
import aspose.pdf as ap
--------------------------------------通过转换图片实现压缩------------------------------------
def convert_img(path1, dpi, work_path):
file = fitz.open(path1)
page_count = file.page_count
if os.path.exists(work_path): # 临时文件夹,需要为空
os.removedirs(work_path)
os.mkdir(work_path)
for i in range(page_count):
page = file[i]
dpi = int(dpi) # 值越大,分辨率越高,文件越清晰
rotate = int(0)
print(page)
trans = fitz.Matrix(dpi / 100.0, dpi / 100.0).prerotate(rotate)
pm = page.get_pixmap(matrix=trans, alpha=False)
img_path = work_path + '/%s.jpg' % str(i + 1)
pm.save(img_path) # 保存为图像文件
file.close()
def img_for_pdf(path1, path2, work_path):
file1 = fitz.open(path1)
page_count = file1.page_count
file2 = fitz.open()
for i in range(page_count):
img_path = work_path + '/%s.jpg' % str(i + 1)
img = fitz.open(img_path) # 打开图片
pdf = img.convert_to_pdf() # 使用图片创建单页的PDF
os.remove(img_path)
pdf_temp = fitz.open("pdf", pdf)
file2.insert_pdf(pdf_temp) # 将当前页插入文档
if os.path.exists(path2): # 若文件存在先删除
os.remove(path2)
file2.save(path2) # 保存PDF文件
file2.close()
def Compress_by_img(path1, path2, dpi, work_path):
"""
:param path1: 需要压缩的pdf文件路径
:param path2: 保存的pdf文件路径
:param dpi: 分辨率
:param work_path: 临时工作路径
:return: None
无水印,效果较好!
"""
convert_img(path1, dpi, work_path)
img_for_pdf(path1, path2, work_path)
os.removedirs(work_path)
if name == "main":
url = "D:\BaiduNetdiskDownload\Download\dd.pdf"
work_path1 = r'D:\BaiduNetdiskDownload\Download.pdf'
in_path = r"D:/BaiduNetdiskDownload/Download/dd.pdf" # 需要压缩的PDF文件
out_path = r"D:\BaiduNetdiskDownload\Download\11.pdf" # 压缩后的PDF文件路径
zoom = 1500 # 清晰度调节,缩放比率
Compress_by_img(in_path, out_path, zoom, work_path1)