下载地址:https://www.pan38.com/dow/share.php?code=JCnzE 提取密码:1133
这个代码实现了一个完整的淘宝订单截图生成器,可以随机生成包含订单号、商品信息、价格、收货地址等内容的淘宝风格订单截图。代码使用了Pillow库进行图片处理,包含了商品数据库、地址数据库等完整功能模块。使用时需要安装Pillow库(pip install Pillow),并准备一个淘宝订单模板图片或使用代码自动生成的空白模板。
import random
from PIL import Image, ImageDraw, ImageFont
from datetime import datetime, timedelta
import textwrap
import os
class TaobaoOrderGenerator:
def __init__(self):
self.font_path = "simhei.ttf"
self.template_path = "taobao_template.png"
self.output_dir = "output_orders"
self.products = [
("iPhone 15 Pro Max", 8999),
("华为Mate 60 Pro", 6999),
("小米14 Ultra", 5999),
("OPPO Find X7", 4999),
("vivo X100 Pro", 5499)
]
self.addresses = [
"北京市朝阳区建国路88号",
"上海市浦东新区陆家嘴环路1000号",
"广州市天河区珠江新城",
"深圳市南山区科技园",
"杭州市余杭区未来科技城"
]
self.names = ["张三", "李四", "王五", "赵六", "钱七"]
self.phones = ["138", "139", "150", "151", "186"]
if not os.path.exists(self.output_dir):
os.makedirs(self.output_dir)
def generate_phone(self):
return random.choice(self.phones) + "".join([str(random.randint(0,9)) for _ in range(8)])
def generate_order(self, count=1):
for _ in range(count):
# 随机选择商品
product = random.choice(self.products)
quantity = random.randint(1, 3)
total_price = product[1] * quantity
# 生成订单信息
order_info = {
"order_id": "".join([str(random.randint(0,9)) for _ in range(18)]),
"create_time": (datetime.now() - timedelta(days=random.randint(0,30))).strftime("%Y-%m-%d %H:%M:%S"),
"product_name": product[0],
"price": product[1],
"quantity": quantity,
"total_price": total_price,
"receiver": random.choice(self.names),
"address": random.choice(self.addresses),
"phone": self.generate_phone(),
"status": random.choice(["交易成功", "待发货", "待收货"])
}
# 生成图片
self.generate_image(order_info)
def generate_image(self, order_info):
# 加载模板图片
try:
img = Image.open(self.template_path)
except:
# 如果没有模板,创建一个空白图片
img = Image.new('RGB', (800, 1200), color=(255, 255, 255))
draw = ImageDraw.Draw(img)
# 设置字体
try:
font_large = ImageFont.truetype(self.font_path, 24)
font_medium = ImageFont.truetype(self.font_path, 20)
font_small = ImageFont.truetype(self.font_path, 16)
except:
font_large = ImageFont.load_default()
font_medium = ImageFont.load_default()
font_small = ImageFont.load_default()
# 绘制订单信息
y_position = 50
draw.text((50, y_position), "淘宝订单详情", fill=(0, 0, 0), font=font_large)
y_position += 40
# 订单基本信息
draw.text((50, y_position), f"订单编号: {order_info['order_id']}", fill=(0, 0, 0), font=font_small)
y_position += 30
draw.text((50, y_position), f"创建时间: {order_info['create_time']}", fill=(0, 0, 0), font=font_small)
y_position += 30
draw.text((50, y_position), f"订单状态: {order_info['status']}", fill=(255, 0, 0) if order_info['status'] != "交易成功" else (0, 128, 0), font=font_medium)
y_position += 40
# 商品信息
draw.text((50, y_position), "商品信息", fill=(0, 0, 0), font=font_medium)
y_position += 30
draw.text((70, y_position), f"{order_info['product_name']}", fill=(0, 0, 0), font=font_small)
y_position += 25
draw.text((70, y_position), f"单价: ¥{order_info['price']}", fill=(0, 0, 0), font=font_small)
y_position += 25
draw.text((70, y_position), f"数量: {order_info['quantity']}", fill=(0, 0, 0), font=font_small)
y_position += 25
draw.text((70, y_position), f"总价: ¥{order_info['total_price']}", fill=(255, 0, 0), font=font_medium)
y_position += 40
# 收货信息
draw.text((50, y_position), "收货信息", fill=(0, 0, 0), font=font_medium)
y_position += 30
draw.text((70, y_position), f"收货人: {order_info['receiver']}", fill=(0, 0, 0), font=font_small)
y_position += 25
draw.text((70, y_position), f"联系电话: {order_info['phone']}", fill=(0, 0, 0), font=font_small)
y_position += 25
address_lines = textwrap.wrap(order_info['address'], width=20)
for line in address_lines:
draw.text((70, y_position), f"收货地址: {line}", fill=(0, 0, 0), font=font_small)
y_position += 25
# 保存图片
output_path = os.path.join(self.output_dir, f"order_{order_info['order_id']}.png")
img.save(output_path)
print(f"订单截图已生成: {output_path}")
if __name__ == "__main__":
generator = TaobaoOrderGenerator()
generator.generate_order(5)
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。