【教学类-36-10】20240905(通义万相)-A4各种大小的鱼制作“吐泡泡的鱼”01版
背景需求
搭档说:“小孩子差不多不哭了,给他们做点操作材料包吧,你打印A4小鱼,让他们在旁边画圆泡泡”。
我之前正好有做“钓鱼”,下载做一些4:3的小鱼。
【教学类-36-09】20240622钓鱼(通义万相)-A4各种大小的鱼_通义万相艺术创作相关文档-CSDN博客文章浏览阅读612次,点赞11次,收藏6次。【教学类-36-09】20240622钓鱼(通义万相)-A4各种大小的鱼_通义万相艺术创作相关文档https://blog.csdn.net/reasonsummer/article/details/139896696?spm=1011.2415.3001.5331
用它插入A4模版,制作小鱼吐泡
代码展示:1条鱼插入居中
'''
小鱼吐泡A4
作者:AI对话大师,阿夏
2025年9月05日'''import osprint('----------第1步:提取所有的幼儿照片的路径------------')
# 文件信息
folder_path = r'C:\Users\jg2yXRZ\OneDrive\桌面\20250905小鱼吐泡'
ppp =folder_path + r'\图片'
qqq = folder_path+r'\零时Word'
os.makedirs(qqq , exist_ok=True)paths=[]
# 过滤:只保留png结尾的图片 31张(多几张备用)
imgs=os.listdir(ppp)
for img in imgs:if img.endswith(".png"):paths.append(ppp+'\\'+img)
# 所有图片的路径
print(paths)
# 提取动物名字倒数第4个字之前的动物名字
print(imgs)print('----------第3步:随机抽取12张图片 ------------')import docx
from docx import Document
from docx.shared import Pt
from docx.shared import RGBColor
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
from docx.oxml.ns import qn
import randomimport os,time
import docx
from docx import Document
from docx.shared import Inches,Cm,Pt
from docx.shared import RGBColor
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
# # from docx.enum.text import WD_VERTICAL_ALIGNMENT
# from docx.enum.table import WD_CELL_VERTICAL_ALIGNMENT #用来设置单元格垂直对齐方式
from docx.oxml.ns import qnfrom docxtpl import DocxTemplate
import pandas as pd
from docx2pdf import convert
from docx.shared import RGBColorpic=['00']
long=['19.32']
wide=['11.24']
# 每4个图片一组进行处理path=[]
grouped_files = [paths[i:i+1] for i in range(0, len(paths), 1)]
for g in grouped_files:p=g*1path.append(p)print(path)
print(len(path))
# 55for nn in range(0,int(len(path))): # 读取图片的全路径 的数量 31张doc = Document(folder_path+r'\鱼模板.docx')table = doc.tables[0] # 4567(8)行for l in range(len(long)):# 单元格坐标a=int(pic[l][0])b=int(pic[l][1])figures=path[nn][l] # 图片的全路径的第一张
## 写入1张大图run=doc.tables[0].cell(a,b).paragraphs[0].add_run() # # 图片位置 第一个表格的0 3 插入照片run.add_picture(r'{}'.format(figures),width=Cm(float(long[l])),height=Cm(float(wide[l])))table.cell(a,b).paragraphs[0].alignment = WD_PARAGRAPH_ALIGNMENT.CENTER #居中 doc.save(qqq+r'\{}.docx'.format('%02d'%nn)) from docx2pdf import convert# docx 文件另存为PDF文件inputFile = qqq+fr'\{nn:02d}.docx' # 要转换的文件:已存在outputFile = qqq+fr'\{nn:02d}.pdf' # 要生成的文件:不存在# 先创建 不存在的 文件f1 = open(outputFile, 'w')f1.close()# 再转换往PDF中写入内容convert(inputFile, outputFile)print('----------第4步:把都有PDF合并为一个打印用PDF------------')# 多个PDF合并(CSDN博主「红色小小螃蟹」,https://blog.csdn.net/yangcunbiao/article/details/125248205)
import os
from PyPDF2 import PdfFileMerger
target_path = qqq
pdf_lst = [f for f in os.listdir(target_path) if f.endswith('.pdf')]
pdf_lst = [os.path.join(target_path, filename) for filename in pdf_lst]
pdf_lst.sort()
file_merger = PdfFileMerger()
for pdf in pdf_lst:print(pdf)file_merger.append(pdf)
file_merger.write(folder_path+fr"\小鱼吐泡A4({len(path)}人共{len(path)}份).pdf")
file_merger.close()
# doc.Close()# print('----------第5步:删除临时文件夹------------')
import shutil
shutil.rmtree(qqq) #递归删除文件夹,即:删除非空文件夹