当前位置: 首页 > news >正文

【办公类-115-01】20250920职称资料上传01——多个jpg转同名PDF(如:荣誉证书)并自动生成单一文件夹

背景需求:

20250920上海市中高职称评审开始,我在2023年申请课题鉴定C,可以有两次参评机会,2023年12月参加过一次考评。2025是第二次。

和2年前一样,在9月有一个职称上报平台的程序。

需要上传很多很多的资料

一、教师账户上传(成绩荣誉证书、个人信息文字、课程表文字)

二、管理园账户(人事档案图片扫描盖章资料上传)

两年前做这个系统上传,里面最大的工作量就是把各种类型的资料转成PDF。足足搞了两周,天天晚上做到9点。

当时我用的是ADOBE AcTOBAT 做的“单张图片转PDF”“多张图片合并PDF”“多个PDF转PDF”、

用word做的docx转PDF

当图片太大,PDF超过5MB或1.5MB,还要用格式工厂压缩图片。在把小图放到ADOBE AcTOBAT 继续合并PDF

特别“结题报告”,要分别把docx转PDF,把图片合并PDF,再把多个合并成一个PDF。ADOBE AcTOBAT 要操作很多次合并。

加上总之非常繁琐、繁杂、烦躁。

操作流程

今年再次做这个系统,我就想能不能用AI写Python代码,直接实现

1、单张图片转PDF(单张荣誉证书jpg转PDF)

2、多个图片合并PDF(五张教师考评表jpg合并一个PDF)

3、多个PDF合并(十三五证书下载就是PDF)

4、其他综合类文件拼PDF(PDF+图片=PDF、word+图片+PDF=PDF)

最开始整理资料,我能做的就是将各类证书、

以”奖励”为例,准备10张证书(扫描件)

根据重要性级别手动‘’编号


'''
职称01,10张证书jpg转PDF,合并同名文件夹
deepseek,阿夏
20250920
'''import os
import img2pdf
from PIL import Image
from pathlib import Path
import iodef convert_each_image_to_pdf(folder_path):"""将文件夹中的每张图片分别转换为同名的PDF文件Args:folder_path (str): 图片文件夹路径"""# 支持的图片格式supported_formats = ('.png', '.jpg', '.jpeg', '.bmp', '.tiff', '.tif', '.webp')# 获取所有图片文件image_files = []for file in Path(folder_path).iterdir():if file.suffix.lower() in supported_formats and file.is_file():image_files.append(file)if not image_files:print("未找到图片文件!")returnprint(f"找到 {len(image_files)} 张图片")# 处理每张图片success_count = 0for img_path in image_files:try:# 生成输出PDF文件名(保持原名,只改扩展名)output_pdf_name = img_path.with_suffix('.pdf')# 检查是否已存在同名PDF文件if output_pdf_name.exists():print(f"跳过 {img_path.name},PDF文件已存在: {output_pdf_name.name}")continue# 处理图片方向with Image.open(img_path) as img:# 转换为RGB模式(避免透明度问题)if img.mode != 'RGB':img = img.convert('RGB')# 获取图片尺寸width, height = img.size# 判断横竖版:宽度大于高度则为横版if width > height:# 横版图片,旋转90度使其在PDF中正确显示rotated_img = img.rotate(90, expand=True)# 保存为临时文件temp_path = f"temp_{img_path.stem}.jpg"rotated_img.save(temp_path, "JPEG", quality=95)# 转换为PDFwith open(output_pdf_name, "wb") as f:f.write(img2pdf.convert(temp_path))# 删除临时文件os.remove(temp_path)else:# 竖版图片,直接转换with open(output_pdf_name, "wb") as f:f.write(img2pdf.convert(str(img_path)))success_count += 1print(f"已转换: {img_path.name} -> {output_pdf_name.name}")except Exception as e:print(f"转换图片 {img_path.name} 时出错: {e}")print(f"转换完成!成功转换 {success_count} 张图片")def convert_each_image_to_pdf_simple(folder_path):"""简化版本:直接将每张图片转换为PDF,不处理横竖版"""# 支持的图片格式supported_formats = ('.png', '.jpg', '.jpeg', '.bmp', '.tiff', '.tif', '.webp')# 获取所有图片文件image_files = []for file in Path(folder_path).iterdir():if file.suffix.lower() in supported_formats and file.is_file():image_files.append(file)if not image_files:print("未找到图片文件!")returnprint(f"找到 {len(image_files)} 张图片")# 处理每张图片success_count = 0for img_path in image_files:try:# 生成输出PDF文件名output_pdf_name = img_path.with_suffix('.pdf')# 检查是否已存在同名PDF文件if output_pdf_name.exists():print(f"跳过 {img_path.name},PDF文件已存在: {output_pdf_name.name}")continue# 直接转换为PDFwith open(output_pdf_name, "wb") as f:f.write(img2pdf.convert(str(img_path)))success_count += 1print(f"已转换: {img_path.name} -> {output_pdf_name.name}")except Exception as e:print(f"转换图片 {img_path.name} 时出错: {e}")print(f"转换完成!成功转换 {success_count} 张图片")if __name__ == "__main__":# 使用示例path = r'C:\Users\Administrator\Desktop\09奖励'folder_path = pathprint("开始转换每张图片为单独的PDF...")# 先尝试简化版本(不处理横竖版)print("尝试简化版本...")convert_each_image_to_pdf_simple(folder_path)# 检查是否还有图片未转换(可能因为横版问题转换失败)# 获取所有图片文件supported_formats = ('.png', '.jpg', '.jpeg', '.bmp', '.tiff', '.tif', '.webp')remaining_images = []for file in Path(folder_path).iterdir():if file.suffix.lower() in supported_formats and file.is_file():pdf_file = file.with_suffix('.pdf')if not pdf_file.exists():  # 如果对应的PDF文件不存在remaining_images.append(file)if remaining_images:print(f"还有 {len(remaining_images)} 张图片未转换,尝试完整版本...")# 对未转换的图片使用完整版本for img_path in remaining_images:try:output_pdf_name = img_path.with_suffix('.pdf')with Image.open(img_path) as img:if img.mode != 'RGB':img = img.convert('RGB')width, height = img.sizeif width > height:rotated_img = img.rotate(90, expand=True)temp_path = f"temp_{img_path.stem}.jpg"rotated_img.save(temp_path, "JPEG", quality=95)with open(output_pdf_name, "wb") as f:f.write(img2pdf.convert(temp_path))os.remove(temp_path)else:with open(output_pdf_name, "wb") as f:f.write(img2pdf.convert(str(img_path)))print(f"已转换: {img_path.name} -> {output_pdf_name.name}")except Exception as e:print(f"转换图片 {img_path.name} 时出错: {e}")

一个代码将每张图片转成一个同名的pdf

一开始就这样,但是上传的时候,我发现所有10个PDF和10个jpg在一起,我很容易选错PDF,要很仔细看编号。

所以我还是参考2023年的方法,把证书放到每个文件夹里,这样看编号更清晰

所以我再写一个代码同名的jpg和PDF,放到一个同名文件夹里


'''
职称01,10张证书jpg和PDF,转入同名文件夹
deepseek,阿夏
20250920
'''import os
import shutil
from pathlib import Pathdef organize_files_by_name(source_folder):"""将同名文件(不同后缀)整理到同名文件夹中Args:source_folder: 源文件夹路径"""source_path = Path(source_folder)# 检查源文件夹是否存在if not source_path.exists():print(f"错误:文件夹 '{source_folder}' 不存在")return# 用于存储文件名和对应文件路径的字典file_groups = {}# 遍历源文件夹中的所有文件for file_path in source_path.iterdir():if file_path.is_file():# 获取文件名(不含后缀)file_stem = file_path.stem# 获取文件后缀file_suffix = file_path.suffix# 将文件按文件名分组if file_stem not in file_groups:file_groups[file_stem] = []file_groups[file_stem].append(file_path)# 创建文件夹并移动文件moved_count = 0for file_stem, file_list in file_groups.items():# 如果同名文件数量大于1,或者用户希望单个文件也整理if len(file_list) > 0:# 创建目标文件夹target_folder = source_path / file_stemtarget_folder.mkdir(exist_ok=True)# 移动文件到目标文件夹for file_path in file_list:target_path = target_folder / file_path.nameshutil.move(str(file_path), str(target_path))print(f"移动: {file_path.name} -> {file_stem}/")moved_count += 1print(f"\n整理完成!共移动了 {moved_count} 个文件")def organize_files_with_preview(source_folder):"""带预览功能的文件整理(先显示将要进行的操作,确认后执行)Args:source_folder: 源文件夹路径"""source_path = Path(source_folder)if not source_path.exists():print(f"错误:文件夹 '{source_folder}' 不存在")returnfile_groups = {}# 收集文件信息for file_path in source_path.iterdir():if file_path.is_file():file_stem = file_path.stemif file_stem not in file_groups:file_groups[file_stem] = []file_groups[file_stem].append(file_path)# 显示预览信息print("预览将要进行的操作:")print("=" * 50)operations = []for file_stem, file_list in file_groups.items():if len(file_list) > 0:target_folder = source_path / file_stemoperations.append({'folder': target_folder,'files': file_list})print(f"创建文件夹: {file_stem}")for file_path in file_list:print(f"  └─ 移动: {file_path.name}")print()if not operations:print("没有找到需要整理的文件")return# 确认是否执行confirm = input("确认执行以上操作?(y/n): ").lower()if confirm == 'y':# 执行移动操作moved_count = 0for op in operations:op['folder'].mkdir(exist_ok=True)for file_path in op['files']:target_path = op['folder'] / file_path.nameshutil.move(str(file_path), str(target_path))moved_count += 1print(f"\n整理完成!共移动了 {moved_count} 个文件")else:print("操作已取消")def main():# 设置源文件夹路径(这里是你的123文件夹)source_folder = r'C:\Users\Administrator\Desktop\09奖励'print("文件整理工具")print("1. 直接执行")print("2. 预览后执行")# choice = input("请选择模式 (1/2): ")choice = "1"if choice == "1":organize_files_by_name(source_folder)elif choice == "2":organize_files_with_preview(source_folder)else:print("无效选择")if __name__ == "__main__":main()

放到单独文件夹内,就只有一个PDF上传,不容易出错。

用这个方法做了“荣誉”和“公开课”

还有部分“教科研成果”(根据图片上的证书名称做文件名,用文件夹名做文件夹,里面可以另外放入word论文报告)

用deepseek编写Python程序,快速将多张图片转为多个同名PDF,并根据文件名转入同名文件夹,大大提高了整理效率。(比用ADOBE,点很多的按钮来转PDF要快很多,只要右击运行两个按钮就可以了。)

http://www.dtcms.com/a/426583.html

相关文章:

  • 基于Kafka+ElasticSearch+MongoDB+Redis+XXL-Job日志分析系统(学习)
  • 【龙泽科技】智能网联汽车智能传感器测试装调仿真教学软件
  • JAVA:Spring Boot 集成 BouncyCastle 实现加密算法
  • 石家庄住房和城乡建设局官方网站app模板下载网站
  • gRPC从0到1系列【9】
  • IDEA 2024 中创建 Maven 项目的详细步骤
  • 2025 AI 图景:从工具革命到生态重构的五大趋势
  • 网站开发者模式下载视频wordpress如何添加备案号
  • UNIX下C语言编程与实践22-UNIX 文件其他属性获取:stat 结构与 localtime 函数的使用
  • UNIX下C语言编程与实践15-UNIX 文件系统三级结构:目录、i 节点、数据块的协同工作机制
  • 青浦做网站的公司网站开发语言html5 php
  • 【分布式中间件】RabbitMQ 功能详解与高可靠实现指南
  • SOME/IP-SD报文结构和交互详解
  • 给贾维斯加“手势控制”:从原理到落地,打造多模态交互的本地智能助
  • 电商数据分析优化清理大师
  • 论文阅读:《Self-Supervised Continual Graph Learning in Adaptive Riemannian Spaces》
  • Qt事件处理全解析
  • 深入理解 LLM 分词器:BPE、WordPiece 与 Unigram
  • 【大模型评估】大模型评估的五类数据
  • 3-2 Windows 安全设置
  • 网站建设平台 汉龙举报个人备案网站做经营性
  • 做技术网站赚钱比较好用的微信社群管理软件
  • DCT与DST变换原理及其在音视频编码中的应用解析
  • 高端网络建站松岗做网站哪家便宜
  • 大连网站设计报价游戏大全免费版入口
  • 长沙人才招聘网站硅谷主角刚开始做的是软件还是网站
  • 网站正能量做网站 人员
  • 做刷票的网站阳山做网站
  • 可以做超链接或锚文本的网站有哪些西安品牌策划公司排名
  • 抽奖网站怎么制作手机端网站的建设