作品展示:
背景需求:
【教学类-09-02】20240331细线迷宫图01(A4横版一页1份横版)-CSDN博客文章浏览阅读779次,点赞28次,收藏6次。【教学类-09-02】20240331细线迷宫图01(A4横版一页1份横版)https://blog.csdn.net/reasonsummer/article/details/137194011?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22137194011%22%2C%22source%22%3A%22reasonsummer%22%7Dhttps://blog.csdn.net/reasonsummer/article/details/137194011?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22137194011%22%2C%22source%22%3A%22reasonsummer%22%7D
设计过程:
一、模板准备
二、代码解析:
三、代码展示:
'''
批量制作细线条的迷宫图(A4横板一面4张横版)
作者:
1、落难Coder https://blog.csdn.net/u014297502/article/details/124839912
2、AI对话大师
3、阿夏
作者:2024年4月1日
'''num=int(input('几人(30人)\n'))print('-----------1、 生成细线迷宫-----------')
import sys
import matplotlib.pyplot as plt
from random import randint
import os# 保存多少张图? 一人4张图
for i in range(num*4):WIDTH = 15HEIGHT = 10sys.setrecursionlimit(WIDTH * HEIGHT)def initVisitedList():visited = []for y in range(HEIGHT):line = []for x in range(WIDTH):line.append(False)visited.append(line)return visiteddef drawLine(x1, y1, x2, y2):plt.plot([x1, x2], [y1, y2], color="black")def removeLine(x1, y1, x2, y2):plt.plot([x1, x2], [y1, y2], color="white")def get_edges(x, y):result = []result.append((x, y, x, y+1))result.append((x+1, y, x+1, y+1))result.append((x, y, x+1, y))result.append((x, y+1, x+1, y+1))return resultdef drawCell(x, y):edges = get_edges(x, y)for item in edges:drawLine(item[0], item[1], item[2], item[3])def getCommonEdge(cell1_x, cell1_y, cell2_x, cell2_y):edges1 = get_edges(cell1_x, cell1_y)edges2 = set(get_edges(cell2_x, cell2_y))for edge in edges1:if edge in edges2:return edgereturn Nonedef initEdgeList():edges = set()for x in range(WIDTH):for y in range(HEIGHT):cellEdges = get_edges(x, y)for edge in cellEdges:edges.add(edge)return edgesdef isValidPosition(x, y):if x < 0 or x >= WIDTH:return Falseelif y < 0 or y >= HEIGHT:return Falseelse:return Truedef shuffle(dX, dY):for t in range(4):i = randint(0, 3)j = randint(0, 3)dX[i], dX[j] = dX[j], dX[i]dY[i], dY[j] = dY[j], dY[i]def DFS(X, Y, edgeList, visited):dX = [0, 0, -1, 1]dY = [-1, 1, 0, 0]shuffle(dX, dY)for i in range(len(dX)):nextX = X + dX[i]nextY = Y + dY[i]if isValidPosition(nextX, nextY):if not visited[nextY][nextX]:visited[nextY][nextX] = TruecommonEdge = getCommonEdge(X, Y, nextX, nextY)if commonEdge in edgeList:edgeList.remove(commonEdge)DFS(nextX, nextY, edgeList, visited)edgeList = initEdgeList()visited = initVisitedList()DFS(0, 0, edgeList, visited)edgeList.remove((0, 0, 0, 1))edgeList.remove((WIDTH, HEIGHT-1, WIDTH, HEIGHT))figure = plt.figure(figsize=(14.85,10.5)) # 创建一个指定大小的图像窗口 29.7变成29 除以2ax = plt.Axes(figure, [0., 0., 1., 1.], frame_on=False) # 创建一个坐标轴,覆盖整个图像窗口figure.add_axes(ax)ax.axis('off') # 关闭坐标轴显示for edge in edgeList:drawLine(edge[0], edge[1], edge[2], edge[3])# 新建图片文件夹,保存所有生成的迷宫图path=r'C:\Users\jg2yXRZ\OneDrive\桌面\细线迷宫图图片'folder_path = path+r'\01迷宫图'os.makedirs(folder_path, exist_ok=True)plt.savefig(folder_path+r'\{}.png'.format('%02d'%i), dpi=400) # 保存迷宫图像为maze.png,设置dpi参数调整图像质量# 生成60张图# 30*2 plt.close() # 超过20张图片会提示占内存,所以关闭图像窗口释放内存
# plt.show()print('-----------2、 导入word,合并png----------')import os,time
from docx import Document
from docx.shared import Cm, RGBColor
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
from docx.oxml.ns import qn
from docx2pdf import convert
from PyPDF2 import PdfMerger# 设置路径和文件夹名称folder_path ='迷宫图所有图片文件夹路径'
path=r'C:\Users\jg2yXRZ\OneDrive\桌面\细线迷宫图图片'
folder_path = path+r'\01迷宫图'
template_path = path+r"\03迷宫图细线(A4横版4张横图).docx"# 模板文件路径
output_path =path+r'\零时Word'# 生成docx和PDF的文件夹,最后要删除# 创建输出文件夹
if not os.path.exists(output_path):os.makedirs(output_path)n = 1filename=[]
# 遍历图片文件夹
for i in range(0, len(os.listdir(folder_path)), 4):filename.clear()for p in range(0,4):filename .append(os.listdir(folder_path)[i+p]) # i、i+1print(filename)# 打开模板文档doc = Document(template_path)# 获取第一个表格table = doc.tables[0]# 获取第一个表格c=0for a in range(0,2):for b in range(0,2): table.cell(a,b).paragraphs[0].add_run().add_picture(os.path.join(folder_path, filename[c]), width=Cm(14.85), height=Cm(10.4))c+=1# 保存为Word文档doc.save(os.path.join(output_path, '{:02d}.docx'.format(n)))time.sleep(3)# 转换为PDFconvert(os.path.join(output_path, '{:02d}.docx'.format(n)), os.path.join(output_path, '{:02d}.pdf'.format(n)))n += 1
# 合并PDF
pdf_lst = [os.path.join(output_path, filename) for filename in os.listdir(output_path) if filename.endswith('.pdf')]
pdf_lst.sort()file_merger = PdfMerger()
for pdf in pdf_lst:file_merger.append(pdf)file_merger.write(path+fr'\03(打印合集)迷宫图(A4横版整页4份横图)({num}份).pdf')
time.sleep(5)file_merger.close()# 删除临时文件夹
import shutil
shutil.rmtree(output_path)
shutil.rmtree(folder_path)
四、作品展示:
五、合并作品