下载地址:https://www.pan38.com/dow/share.php?code=JCnzE 提取密码:2718
这段代码实现了一个完整的淘宝订单生成器,包含以下功能:1. 生成随机订单号和时间 2. 支持添加多个商品 3. 自动计算总金额 4. 生成订单二维码 5. 输出高质量订单图片。使用时需要准备中文字体文件和背景模板。
import random
import datetime
from PIL import Image, ImageDraw, ImageFont
import textwrap
import qrcode
import os
class TaobaoOrderGenerator:
def init(self):
self.font_path = "simhei.ttf"
self.template_path = "template.jpg"
self.output_dir = "output"
self.products = []
self.init_fonts()
self.init_template()
def init_fonts(self):
self.font_small = ImageFont.truetype(self.font_path, 18)
self.font_medium = ImageFont.truetype(self.font_path, 22)
self.font_large = ImageFont.truetype(self.font_path, 28)
self.font_title = ImageFont.truetype(self.font_path, 36)
def init_template(self):
if not os.path.exists(self.template_path):
# 创建空白模板
self.template = Image.new('RGB', (750, 1334), (245, 245, 245))
draw = ImageDraw.Draw(self.template)
# 绘制顶部导航栏
draw.rectangle([(0, 0), (750, 88)], fill=(255, 255, 255))
draw.text((30, 40), "订单详情", font=self.font_title, fill=(0, 0, 0))
# 绘制订单卡片
draw.rectangle([(20, 100), (730, 400)], fill=(255, 255, 255), outline=(230, 230, 230))
else:
self.template = Image.open(self.template_path)
def add_product(self, name, price, count, sku):
self.products.append({
"name": name,
"price": price,
"count": count,
"sku": sku
})
def generate_order_info(self):
order_no = "".join([str(random.randint(0,9)) for _ in range(18)])
create_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
total_amount = sum(p["price"]*p["count"] for p in self.products)
payment_method = random.choice(["支付宝", "微信支付", "花呗分期"])
return {
"order_no": order_no,
"create_time": create_time,
"total_amount": total_amount,
"payment_method": payment_method
}
def generate_qrcode(self, text):
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=6,
border=2,
)
qr.add_data(text)
qr.make(fit=True)
return qr.make_image(fill_color="black", back_color="white")
def draw_product_list(self, draw, y_start):
y = y_start
for product in self.products:
# 商品名称 (自动换行)
lines = textwrap.wrap(product["name"], width=20)
for line in lines:
draw.text((40, y), line, font=self.font_medium, fill=(0, 0, 0))
y += 30
# 商品SKU和数量
sku_text = f"规格: {product['sku']} 数量: {product['count']}"
draw.text((40, y), sku_text, font=self.font_small, fill=(150, 150, 150))
y += 30
# 商品价格
price_text = f"¥{product['price']:.2f}"
draw.text((650, y-60), price_text, font=self.font_medium, fill=(255, 80, 0))
# 分割线
draw.line([(30, y+10), (720, y+10)], fill=(230, 230, 230), width=1)
y += 30
return y
def generate(self):
if not self.products:
raise ValueError("请先添加商品")
order_info = self.generate_order_info()
img = self.template.copy()
draw = ImageDraw.Draw(img)
# 绘制订单基本信息
draw.text((40, 120), f"订单编号: {order_info['order_no']}", font=self.font_small, fill=(100, 100, 100))
draw.text((40, 150), f"创建时间: {order_info['create_time']}", font=self.font_small, fill=(100, 100, 100))
# 绘制商品列表
product_y = self.draw_product_list(draw, 200)
# 绘制订单总金额
total_text = f"实付款: ¥{order_info['total_amount']:.2f}"
draw.text((40, product_y+30), total_text, font=self.font_large, fill=(255, 80, 0))
# 绘制支付方式
pay_text = f"支付方式: {order_info['payment_method']}"
draw.text((40, product_y+80), pay_text, font=self.font_medium, fill=(100, 100, 100))
# 添加二维码
qr_text = f"淘宝订单{order_info['order_no']}"
qr_img = self.generate_qrcode(qr_text)
img.paste(qr_img, (550, product_y+50))
# 保存图片
if not os.path.exists(self.output_dir):
os.makedirs(self.output_dir)
output_path = os.path.join(self.output_dir, f"order_{order_info['order_no']}.jpg")
img.save(output_path)
return output_path
使用示例
if name == "main":
generator = TaobaoOrderGenerator()
# 添加商品
generator.add_product("Apple iPhone 15 Pro Max 1TB 原色钛金属", 12999, 1, "1TB|原色钛金属")
generator.add_product("小米14 Ultra 16GB+1TB 黑色 套装版", 7999, 1, "16GB+1TB|黑色|套装")
generator.add_product("华为Mate60 Pro+ 12GB+512GB 丹霞橙", 8999, 1, "12GB+512GB|丹霞橙")
# 生成订单图片
output_path = generator.generate()
print(f"订单已生成: {output_path}")