python 压缩jpg、PDF文件的大小,可根据参数进行压缩
python 压缩jpg、PDF文件的大小,可根据参数进行压缩
-- coding: utf-8 --
import sys, fitz
import os
import stat
import shutil
import datetime
from PIL import Image
import re
import winerror
from win32com.client.dynamic import Dispatch, ERRORS_BAD_CONTEXT
from PyQt5 import QtCore, QtGui, QtWidgets
import cv2, glob
from PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog
def pdf_jpg(pdfPath, tem):
ERRORS_BAD_CONTEXT.append(winerror.E_NOTIMPL)
my_dir = r'D:jpg\' + str(tem)
if not os.path.exists(my_dir):#判断存放图片的文件夹是否存在
os.makedirs(my_dir)
# my_pdf = "1.pdf"
# #os.chdir(my_dir)
# src = os.path.abspath(my_pdf)
#print(pdfPath)
try:
AvDoc = Dispatch("AcroExch.AVDoc")
if AvDoc.Open(pdfPath, ""):
pdDoc = AvDoc.GetPDDoc()
jsObject = pdDoc.GetJSObject()
jsObject.SaveAs(os.path.join(my_dir, '1.jpeg'), "com.adobe.acrobat.jpeg")
except Exception as e:
print(str(e))
finally:
AvDoc.Close(True)
jsObject = None
pdDoc = None
AvDoc = None
def natural_sort_key(s):
_nsre = re.compile(‘([0-9]+)’)
return [int(text) if text.isdigit() else text.lower() for text in re.split(_nsre, s)]
def yes(pdf_name1, tem):
img_path = r’D:jpg’ + str(tem)
doc = fitz.open()
new_pic = os.listdir(img_path)
new_pic.sort(key=natural_sort_key)
# 循环path中的文件,可import os 然后用 for img in os.listdir(img_path)实现
# 这里为了让文件以1,2,3的形式进行拼接,就偷懒循环文件名中的数字。
ii = 1
for i in new_pic:
img_file = os.path.join(img_path, i)
imgdoc = fitz.open(img_file)#打开照片路径
pdfbytes = imgdoc.convertToPDF()
pdf_name = os.path.join(img_path, ‘%s.pdf’%ii)
imgpdf = fitz.open(pdf_name, pdfbytes)
doc.insertPDF(imgpdf)
ii += 1
doc.save(pdf_name1)
doc.close()
#print(pdf_name1)
def all_files_path(df_path):
filepaths = []
for root, dirs, files in os.walk(df_path): # 分别代表根目录、文件夹、文件
for file in files: # 遍历文件
file_path = os.path.join(root, file) # 获取文件绝对路径
#print(file_path)
if file_path[-4:].lower() == ‘.pdf’:
filepaths.append(file_path) # 将文件路径添加进列表
for dir in dirs: # 遍历目录下的子目录
dir_path = os.path.join(root, dir) # 获取子目录路径
all_files_path(dir_path)
return filepaths
def all_jpf_path(df_path):
filepaths = []
for root, dirs, files in os.walk(df_path): # 分别代表根目录、文件夹、文件
for file in files: # 遍历文件
file_path = os.path.join(root, file) # 获取文件绝对路径
#print(file_path)
if file_path[-5:].lower() == ‘.jpeg’:
filepaths.append(file_path) # 将文件路径添加进列表
for dir in dirs: # 遍历目录下的子目录
dir_path = os.path.join(root, dir) # 获取子目录路径
all_jpf_path(dir_path)
return filepaths
def delete():
filePath = r’D:jpg’
if os.path.exists(filePath):
for fileList in os.walk(filePath):
for name in fileList[2]:
try:
os.chmod(os.path.join(fileList[0],name), stat.S_IWRITE)
os.remove(os.path.join(fileList[0],name))
except:
pass
try:
shutil.rmtree(filePath)
except:
pass
return “delete ok”
else:
return “no filepath”
def main():
pdfPath = r’C:UsersyeDesktop新建文件夹 (6)新建文件夹 (2)’
pdf_name = r’C:UsersyeDesktop新建文件夹 (6)新建文件夹 (3)’
list_pdf = all_files_path(pdfPath)
#delete()
#print(list_pdf)
i = 1
for path_pdf in list_pdf:
pdf_jpg(path_pdf, i)
path_pdf_h = path_pdf.replace(pdfPath, pdf_name)
a,b = os.path.split(path_pdf_h)
if os.path.exists(a) == False:
os.makedirs(a)
yes(path_pdf_h, i)
i += 1
#delete()
def cx2_jpd(jpg_dx):
list_jpg = all_jpf_path(r’D:jpg1’)
i = 1
for path_jpg in list_jpg:
a, b = os.path.split(path_jpg)
os.rename(path_jpg,os.path.join(a, ‘%s.jpeg’%i))
i += 1
for ii in range(1, i):
path_jpg = os.path.join(r’D:jpg1’, ‘%s.jpeg’%ii)
img = cv2.imread(path_jpg.replace(‘/’, ‘’),1)
cv2.imwrite(path_jpg, img,[cv2.IMWRITE_JPEG_QUALITY, int(jpg_dx)])
def ye(self, row, col, text):
# _translate = QtCore.QCoreApplication.translate
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setItem(row, col, item)
item = self.tableWidget.item(row, col)
item.setText(text)
QApplication.processEvents()
def formatSize(bytes):
try:
bytes = float(bytes)
kb = round(bytes / 1024,2)
except:
print(“传入的字节格式不对”)
return “Error”
if kb >= 1024:
M = round(kb / 1024,2)
if M >= 1024:
G = round(M / 1024,2)
return "%sG" % (G)
else:
return "%sM" % (M)
else:
return "%skb" % (kb)
def getDocSize(path):
try:
size = os.path.getsize(path)
return formatSize(size)
except Exception as err:
print(err)
def main_pdf(self, path_ys, name_pdf, name_path, name_path1, xs, tem):
path_pdf = os.path.join(name_path, name_pdf)
delete()
pdf_jpg(path_pdf, 1)
cx2_jpd(xs)
print(path_pdf, path_ys, name_path1)
path_pdf_h = path_pdf.replace(‘’,‘/’).replace(path_ys, name_path1)
a,b = os.path.split(path_pdf_h)
if os.path.exists(a) == False:
os.makedirs(a)
yes(path_pdf_h, 1)
ye(self, tem, 3, ‘%s/%s’%(getDocSize(path_pdf), getDocSize(path_pdf_h)))
#delete()
if name == “main”:
main()