一款专门解决Windows打印没有6张8张布局脚本(Windows 自带打印图片布局里面)
一款专门解决Windows打印没有6张8张布局脚本(Windows 自带打印图片布局里面)Windows 自带的图片打印界面没有内置 6 张(2×3 或 3×2)的布局选项,
只提供 1、2、4、9 张等预设。这是系统设计限制
所以需要本脚本
使用教程:直接把需要打印排版的图片,放到桌面“”新建文件夹“(没有可以自己新建一个)”
然后双击脚本或者exe软件
弹出如下,自己可以选着需要的布局;直接回车,也可以直接按回车默认6张布局。
代码如下:
import os
from PIL import Image
# ===================== 基础打印配置 =====================
# A4 300DPI 标准尺寸
A4_W = 2480
A4_H = 3508
DPI = 300
CM2PX = DPI / 2.54
MM2PX = CM2PX / 10
# 四周统一固定 2.5mm 小白边(永久生效)
SMALL_MM = 2.5
EDGE_MARGIN = int(SMALL_MM * MM2PX)
# 3厘米大留白
MARGIN_3CM = int(3 * CM2PX)
# 图片间隙
GAP = 10
BG_COLOR = (255, 255, 255)
# 路径配置
desktop = os.path.join(os.path.expanduser("~"), "Desktop")
SRC_FOLDER = os.path.join(desktop, "新建文件夹")
OUT_FOLDER = os.path.join(desktop, "A4批量排版输出")
# 全部排版规格
LAYOUT_INFO = {
2:(1, 2),
4:(2, 2),
6:(2, 3),
8:(2, 4)
}
# ========================================================
def select_need_layout():
"""自选排版,直接回车默认6张"""
print("=" * 55)
print("请选择需要生成的排版(默认直接回车 = 只生成6张)")
print("1 = 每页 2 张")
print("2 = 每页 4 张")
print("3 = 每页 6 张【默认】")
print("4 = 每页 8 张")
print("多选用逗号隔开,例如:1,3")
print("=" * 55)
inp = input("请输入选择:").strip()
# 直接回车 默认6张
if not inp:
return
selected = []
for s in inp.split(","):
s = s.strip()
if s == "1":
selected.append(2)
elif s == "2":
selected.append(4)
elif s == "3":
selected.append(6)
elif s == "4":
selected.append(8)
if not selected:
return
return selected
def choose_margin_mode():
"""留白选择,直接回车默认四周2.5mm无大留白"""
print("-" * 55)
print("请选择额外大留白(直接回车 = 默认四周2.5mm小边距)")
print("1 → 额外上方留白 3cm")
print("2 → 额外下方留白 3cm")
print("3 → 仅四周2.5mm小边距【默认】")
print("-" * 55)
m = input("输入序号 1/2/3:").strip()
if m == "1":
return 1
elif m == "3" or not m:
return 3
else:
return 2
def get_image_files():
"""读取桌面 新建文件夹 内所有图片"""
suffix = (".jpg", ".jpeg", ".png", ".bmp")
if not os.path.exists(SRC_FOLDER):
print("❌ 错误:桌面未找到【新建文件夹】,请先创建!")
return []
img_list = []
for f in os.listdir(SRC_FOLDER):
if f.lower().endswith(suffix):
img_list.append(os.path.join(SRC_FOLDER, f))
if not img_list:
print("❌ 新建文件夹内没有图片!")
return img_list
def resize_fit(img, tar_w, tar_h):
"""图片等比例缩放+居中,不变形"""
ow, oh = img.size
scale = min(tar_w / ow, tar_h / oh)
nw = int(ow * scale)
nh = int(oh * scale)
img = img.resize((nw, nh), Image.Resampling.LANCZOS)
new_img = Image.new("RGB", (tar_w, tar_h), BG_COLOR)
new_img.paste(img, ((tar_w - nw) // 2, (tar_h - nh) // 2))
return new_img
def start_export(img_list, select_list, margin_mode):
os.makedirs(OUT_FOLDER, exist_ok=True)
# 基础四周2.5mm固定边距
base_top = EDGE_MARGIN
base_bot = EDGE_MARGIN
base_left = EDGE_MARGIN
base_right = EDGE_MARGIN
# 叠加额外大留白
if margin_mode == 1:
top_big = MARGIN_3CM
bot_big = 0
mode_name = "上额外3cm留白"
elif margin_mode == 2:
top_big = 0
bot_big = MARGIN_3CM
mode_name = "下额外3cm留白"
else:
top_big = 0
bot_big = 0
mode_name = "仅四周2.5mm边距"
final_top = base_top + top_big
final_bot = base_bot + bot_big
for per_page in select_list:
rows, cols = LAYOUT_INFO
sub_dir = os.path.join(OUT_FOLDER, f"{per_page}张_{mode_name}")
os.makedirs(sub_dir, exist_ok=True)
use_w = A4_W - base_left - base_right
use_h = A4_H - final_top - final_bot
cell_w = int((use_w - (cols - 1) * GAP) / cols)
cell_h = int((use_h - (rows - 1) * GAP) / rows)
page_idx = 0
for i in range(0, len(img_list), per_page):
batch = img_list
a4 = Image.new("RGB", (A4_W, A4_H), BG_COLOR)
for idx, path in enumerate(batch):
try:
im = Image.open(path).convert("RGB")
im = resize_fit(im, cell_w, cell_h)
r = idx // cols
c = idx % cols
x = base_left + c * (cell_w + GAP)
y = final_top + r * (cell_h + GAP)
a4.paste(im, (x, y))
except Exception:
print(f"⚠️ 跳过损坏图片: {os.path.basename(path)}")
page_idx += 1
save_name = f"{per_page}张_第{page_idx}页.jpg"
save_path = os.path.join(sub_dir, save_name)
a4.save(save_path, quality=100)
print(f"✅ 【{per_page}张排版】生成完成 → {sub_dir}")
if __name__ == "__main__":
pics = get_image_files()
if not pics:
input("\n按回车退出...")
exit()
print(f"📸 共读取图片:{len(pics)} 张\n")
select_layout = select_need_layout()
mar_mode = choose_margin_mode()
print("\n⏳ 开始生成选中排版,请稍候...\n")
start_export(pics, select_layout, mar_mode)
print("\n🎉 全部任务完成!")
print(f"📂 文件统一输出至桌面:【A4批量排版输出】")
input("\n按回车键关闭窗口...")代码附件如下:
嫌py麻烦的可以直接下载exe软件双击直接运行
exe附件下载地址:
\网站附件\一键6图.exe
页:
[1]