【教学类-76-01】20240820书包01(图案最大化)

时间:2025-03-31 22:30:25
  • '''
  • 书包两片 垂直堆成
  • 星火讯飞 阿夏
  • 20240820
  • '''
  • # print('----1、制作左右翻转图------')
  • import os
  • from PIL import Image
  • import os,time
  • import shutil
  • from docx import Document
  • from import Cm
  • from PIL import Image
  • from PyPDF2 import PdfFileMerger, PdfFileReader
  • from PIL import Image, ImageDraw, ImageFont
  • import os,random
  • # def flip_images(src_folder, dst_folder):
  • # if not (dst_folder):
  • # (dst_folder)
  • # for file in (src_folder):
  • # if ().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp')):
  • # img = ((src_folder, file))
  • # flipped_img = (Image.FLIP_LEFT_RIGHT)
  • # flipped_img.save((dst_folder, file))
  • path=r'C:\Users\jg2yXRZ\OneDrive\图片\20240816书包'
  • src_folder = path+r'\06统一图'
  • dst_folder = path+r'\07翻转图'
  • (dst_folder,exist_ok=True)
  • # flip_images(src_folder, dst_folder)
  • print('----2、图片路径组合------')
  • # 获取文件夹下的所有图片文件
  • left_files = [(src_folder, f) for f in (src_folder) if (('.jpg', '.jpeg', '.png'))]
  • right_files = [(dst_folder, f) for f in (dst_folder) if (('.jpg', '.jpeg', '.png'))]
  • all=[]
  • for i in range(len(left_files)):
  • all.append(left_files[i])
  • all.append(right_files[i])
  • print(len(all))
  • #126*2=252
  • # 将图片拆成6个一组
  • grouped_files = [all[i:i + 2] for i in range(0, len(all), 2)]
  • print(len(grouped_files))
  • # 42张
  • print('----3、合并PDF------')
  • # 创建临时文件夹
  • new_folder = path+r'\零时文件夹'
  • (new_folder, exist_ok=True)
  • print('----3、插入docx,制作pdf------------')
  • # 处理每一组图片
  • for group_index, group in enumerate(grouped_files):
  • # 创建新的Word文档
  • doc = Document(path+r'\书包')
  • # print(group)
  • # 遍历每个单元格,并插入图片
  • for cell_index, image_file in enumerate(group):
  • # 计算图片长宽(单位:厘米)
  • # 如果是空格,就跳过
  • if not image_file:
  • continue
  • # 插入图片到单元格
  • table = [0]
  • # cell = (int(cell_index / 1), cell_index % 1)
  • cell = (int(cell_index / 2), cell_index % 2)
  • # 如果第一行有2个格子,两个数字都写2
  • cell_paragraph = [0]
  • cell_paragraph.clear()
  • run = cell_paragraph.add_run()
  • # run.add_picture(image_file, width=Cm(19.51), height=Cm(14.11))
  • run.add_picture(image_file, width=Cm(14.11), height=Cm(19.51))
  • # 保存Word文档
  • ((new_folder, f'{group_index + 1:03d}.docx'))
  • # 将10个docx转为PDF
  • import os
  • from docx2pdf import convert
  • from PyPDF2 import PdfFileMerger
  • # pdf_output_path = path+fr'\\01书包配对横{int(len(left_files))}张共{int(len(left_files))}图.pdf'
  • pdf_output_path = path+fr'\\01书包配对竖{int(len(left_files))}张共{int(len(left_files))}图.pdf'
  • # 将所有DOCX文件转换为PDF
  • for docx_file in (new_folder):
  • if docx_file.endswith('.docx'):
  • docx_path = (new_folder, docx_file)
  • convert(docx_path, docx_path.replace('.docx', '.pdf'))
  • (2)
  • # 合并零时文件里所有PDF文件
  • merger = PdfFileMerger()
  • for pdf_file in (new_folder):
  • if pdf_file.endswith('.pdf'):
  • pdf_path = (new_folder, pdf_file)
  • (pdf_path)
  • (2)
  • # 保存合并后的PDF文件
  • (pdf_output_path)
  • ()
  • # 删除输出文件夹(删除零时文件夹和水平翻转图片文件夹)
  • (new_folder)
  • # (dst_folder)
  • (2)