背景需求
【教学类65-01】20240622秘密花园涂色书01(通义万相)(A4横版2张,一大3小 38张纸76份)-CSDN博客文章浏览阅读118次。【教学类65-01】20240622秘密花园涂色书01(通义万相)(A4横版2张,一大3小 38张纸76份)https://blog.csdn.net/reasonsummer/article/details/139899797https://blog.csdn.net/reasonsummer/article/details/139899797https://blog.csdn.net/reasonsummer/article/details/139899797
【教学类65-02】20240622秘密花园涂色书02(通义万相)(A4横版2张,一大 68张纸136份)-CSDN博客文章浏览阅读564次。【教学类65-02】20240622秘密花园涂色书02(通义万相)(A4横版2张,一大 68张纸136份)https://blog.csdn.net/reasonsummer/article/details/139904801?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22139904801%22%2C%22source%22%3A%22reasonsummer%22%7Dhttps://blog.csdn.net/reasonsummer/article/details/139904801?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22139904801%22%2C%22source%22%3A%22reasonsummer%22%7D
【教学类65-03】20240622秘密花园涂色书03(通义万相)(A4横版1张,一大 68张纸136份)-CSDN博客文章浏览阅读362次,点赞11次,收藏4次。20240622秘密花园涂色书03(通义万相)(A4横版1张,一大 68张纸136份)https://blog.csdn.net/reasonsummer/article/details/139912622?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22139912622%22%2C%22source%22%3A%22reasonsummer%22%7D
以上都是正方形的图纸1:1
以下我用16:9下载横向长方形
素材准备
图片准备:
代码展示
'''
秘密花园涂色书(通义16:9图,模板1图)
作者:AI对话大师,阿夏
2024年6月22日
'''
import docx
import os
import time
import random
from docx import Document
from docx.shared import Pt, Inches, Cm, RGBColor
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
from docx.enum.text import WD_ALIGN_PARAGRAPH
# from docx.enum.text import WD_VERTICAL_ALIGNMENT
# from docx.enum.table import WD_CELL_VERTICAL_ALIGNMENT
from docx.oxml.ns import qn
from docxtpl import DocxTemplate
import pandas as pd
from docx2pdf import convert
print('----------第1步:提取所有的幼儿照片的路径------------')
# 文件信息
path = r'C:\Users\jg2yXRZ\OneDrive\桌面\秘密花园涂色书'
image_folder=path+r'\00横版jpg'
# 新建文佳佳
ten_folder = path+r'\零时Word'
os.makedirs(ten_folder , exist_ok=True)
# 6个文件夹
image_files = [os.path.join(image_folder, file) for file in os.listdir(image_folder) if file.endswith('.png')]
grouped_files = [image_files[i:i+1] for i in range(0, len(image_files), 1)]
print(grouped_files)
# # 55
pic=['10']
long=['28.26']
wide=['15.9']
# 每4个图片一组进行处理
s=1
for nn in range(0,int(len(grouped_files))): # 读取图片的全路径 的数量 31张
doc = Document(path+r'\04涂色书模板横版图1张.docx')
table = doc.tables[0] # 4567(8)行
# 假设字体名称为"Your Font Name"
font_name = r"C:\Windows\Fonts\FZMWFont.ttf"
for t in [0]:
# 设置单元格内容
cell_0_0 = table.cell(0, t)
cell_0_0.text = f"秘密花园涂色书——{s:03d}"
cell_0_0.paragraphs[0].alignment = docx.enum.text.WD_ALIGN_PARAGRAPH.CENTER
cell_0_0.paragraphs[0].runs[0].font.size = Pt(30) # 设置字体大小
cell_0_0.paragraphs[0].runs[0].font.name = font_name # 设置字体名称
cell_0_0.paragraphs[0].runs[0].font.bold = True # 设置字体加粗
s += 1
for l in range(len(long)):
# 单元格坐标
a=int(pic[l][0])
b=int(pic[l][1])
figures=grouped_files[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(ten_folder+r'\{}.docx'.format('%03d'%nn))
# time.sleep(5)
from docx2pdf import convert
# docx 文件另存为PDF文件
inputFile = ten_folder+fr'\{nn:03d}.docx' # 要转换的文件:已存在
outputFile = ten_folder+fr'\{nn:03d}.pdf' # 要生成的文件:不存在
# 先创建 不存在的 文件
f1 = open(outputFile, 'w')
f1.close()
# 再转换往PDF中写入内容
convert(inputFile, outputFile)
time.sleep(4)
print('----------第4步:把都有PDF合并为一个打印用PDF------------')
# 多个PDF合并(CSDN博主「红色小小螃蟹」,https://blog.csdn.net/yangcunbiao/article/details/125248205)
import os
from PyPDF2 import PdfFileMerger
target_path = ten_folder
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(path+fr"\04涂色书横版1图({len(image_files)}人共{int(len(image_files))}份).pdf")
file_merger.close()
# doc.Close()
# print('----------第5步:删除临时文件夹------------')
import shutil
shutil.rmtree(ten_folder) #递归删除文件夹,即:删除非空文件夹
结果展示