Python把扫描全能王生成的图片合成pdf_全能扫描王怎么把图片转换成pdf,用代码-CSDN博客
对前段时间写的程序进行改进。用手机扫描全能王,用图片格式分享到微信,如果是10来张图片以上就会自己打包成为一个zip文件,把这个zip文件复制到电脑,再用我下面这个程序,拖拉到程序的窗口就可以,将这个zip里面的所有图片按顺序转化为一个pdf文件。
# -*- coding: utf-8 -*-
"""
Created on Sun Feb 4 14:49:19 2024
@author: YBK
"""
import tkinter as tk
import windnd
from tkinter.messagebox import showinfo
from PIL import Image
import os
import glob
import time
import zipfile
# 防止字符串乱码
os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'
def unzip_file(zip_path, extract_to):
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
zip_ref.extractall(extract_to)
# 转化图片为统一宽度
def resize_images(input_dir, output_dir, width, height):
for filename in glob.glob(os.path.join(input_dir, '*.jpg')): # 假设处理的是jpg图片,你可以根据需要修改文件扩展名
image = Image.open(filename)
kgb = image.width / image.height # 图片的宽高比
bzkgb = width / height # 要转化图片的宽高比
paste_w = 0 #粘贴图片在画布上的位置,以左上角为原点 初设x为0
paste_h = 0 #初设y为0
if kgb <= bzkgb:
resized_image = image.resize((int(image.width * (height / image.height)), height)) # 保持宽高比,根据高度调整宽度
paste_w = int((width - int(image.width * (height / image.height))) / 2)
else:
resized_image = image.resize((width, int(image.height * (width / image.width)))) # 保持宽高比,根据宽度调整高度
paste_h = int((height - int(image.height * (width / image.width))) / 2)
canvasnew = Image.new('RGB', (width, height), 'white') #创建一块画布
canvasnew.paste(resized_image, (paste_w, paste_h)) # 居中粘贴图片
output_path = os.path.join(output_dir, os.path.basename(filename))
image.close()
canvasnew.save(output_path) # 保存调整尺寸后的图片
canvasnew.close()
# 转换为pdf
def pic2pdf(img_path, pdf_path):
file_list = os.listdir(img_path)
sources = []
jpg_files = []
replacestr = ''
for file in file_list:
if 'png' in file or 'jpg' in file:
jpg_files.append(file)
if replacestr == '':
replacestr = os.path.basename(file).split('_')[0] + '_'
jpg_files.sort(key=lambda x: int(x.replace(replacestr,'').split('.')[0]))
output = Image.open(img_path + jpg_files[0])
jpg_files.pop(0)
for file in jpg_files:
jpg_file = Image.open(img_path + file)
if jpg_file.mode == "RGB":
jpg_file = jpg_file.convert("RGB")
sources.append(jpg_file)
output.save(pdf_path, "pdf", save_all=True, append_images=sources)
def dec_to_36(num):
base = [str(x) for x in range(10)] + [chr(x) for x in range(ord('A'),ord("A")+26)]
# 前者把 0 ~ 9 转换成字符串存进列表 base 里,后者把 A ~ Z 存进列表
l = []
if num<0:
return "-"+dec_to_36(abs(num))
while True:
num,rem = divmod(num,36) # 求商 和 留余数
l.append(base[rem])
if num == 0:
return "".join(l[::-1])
def nowtime_to_str():
#将当前时间戳转化为36进制,约6位字符,减少文件名长度
unix_timestamp = int(time.time())
return(dec_to_36(unix_timestamp))
def dragged_files(files):
fileurl = ''
if len(files) > 1:
# print("请拖放一个文件!")
showinfo("提示","请拖放一个文件!")
else:
# print(files[0].decode('gbk'))
fileurl = files[0].decode('gbk')
# print(os.path.splitext(fileurl)[1])
if fileurl != '' and os.path.splitext(fileurl)[1] == '.zip':
zippath = fileurl
zip_file_path = zippath # 替换为你的zip文件路径
filename0 = os.path.basename(fileurl).replace('.zip','') + nowtime_to_str()
extract_to_path = f'e:\\临时文件夹\\{filename0}\\' # 指定解压后文件存放的目录
unzip_file(zip_file_path, extract_to_path)
# 待转换图像路径
img_path = f'e:\\临时文件夹\\{filename0}\\'
# 转换后的pdf
pdf_path = fileurl.replace('.zip','.pdf')
# 使用示例:将"input_directory"目录下的所有PNG图片调整为宽度为200像素,并保存到"output_directory"目录中
resize_images(img_path, img_path, 1980, 2800) # 打印A4纸210*297 按这个比例图片是1980*2800
pic2pdf(img_path=img_path, pdf_path=pdf_path)
print('生成PDF文件位置:' + pdf_path)
if __name__ == '__main__':
rootWindow = tk.Tk()
rootWindow.title("拖放zip文件生成pdf")
rootWindow.geometry("300x120")
windnd.hook_dropfiles(rootWindow , func=dragged_files)
rootWindow.mainloop()