如何用脚本生成电子印章

wen 实用脚本 1

本文目录导读:

如何用脚本生成电子印章

  1. Python 方式(最推荐)
  2. JavaScript (HTML+Canvas) 方式
  3. Python 使用 reportlab(更专业)
  4. 安装依赖
  5. 命令行工具(批处理方式)
  6. 使用建议
  7. 注意事项

我来介绍几种生成电子印章的脚本方法,包括Python、JavaScript和批处理方式。

Python 方式(最推荐)

使用 Pillow 和 reportlab

from PIL import Image, ImageDraw, ImageFont
import math
def create_seal(text, output_path='seal.png'):
    # 创建圆形印章
    size = 400
    image = Image.new('RGBA', (size, size), (255, 255, 255, 0))
    draw = ImageDraw.Draw(image)
    # 设置参数
    center = size // 2
    radius = 180
    font_size = 36
    # 尝试加载中文字体
    try:
        font = ImageFont.truetype('msyh.ttc', font_size)  # Windows 微软雅黑
    except:
        font = ImageFont.load_default()  # 备用字体
    # 绘制外圈
    draw.ellipse([20, 20, size-20, size-20], outline='red', width=3)
    draw.ellipse([30, 30, size-30, size-30], outline='red', width=1)
    # 绘制五角星
    star_points = []
    for i in range(5):
        angle = -90 + i * 72  # 五角星角度
        x = center + 50 * math.cos(math.radians(angle))
        y = center + 50 * math.sin(math.radians(angle))
        star_points.append((x, y))
    # 连接五角星的点
    draw.polygon(star_points, outline='red', fill='red')
    # 添加文字(沿圆弧)
    text = text
    angle_step = 360 / len(text)
    for i, char in enumerate(text):
        angle = -90 + i * angle_step
        x = center + 130 * math.cos(math.radians(angle))
        y = center + 130 * math.sin(math.radians(angle))
        # 保存状态并旋转
        char_img = Image.new('RGBA', (font_size, font_size), (0, 0, 0, 0))
        char_draw = ImageDraw.Draw(char_img)
        char_draw.text((0, 0), char, fill='red', font=font)
        char_img = char_img.rotate(-angle, expand=True)
        # 放置文字
        image.paste(char_img, (int(x - char_img.width/2), int(y - char_img.height/2)), char_img)
    image.save(output_path)
    print(f"印章已生成: {output_path}")
# 使用示例
create_seal("示例科技有限公司")

JavaScript (HTML+Canvas) 方式

<!DOCTYPE html>
<html>
<head>电子印章生成器</title>
    <style>
        body { font-family: 'Microsoft YaHei', sans-serif; }
        canvas { border: 1px solid #ccc; }
    </style>
</head>
<body>
    <div>
        <input type="text" id="sealText" value="示例科技有限公司">
        <button onclick="generateSeal()">生成印章</button>
    </div>
    <canvas id="sealCanvas" width="400" height="400"></canvas>
    <script>
    function generateSeal() {
        const canvas = document.getElementById('sealCanvas');
        const ctx = canvas.getContext('2d');
        const text = document.getElementById('sealText').value;
        // 清除画布
        ctx.clearRect(0, 0, canvas.width, canvas.height);
        const centerX = canvas.width / 2;
        const centerY = canvas.height / 2;
        const radius = 160;
        // 绘制外圆
        ctx.beginPath();
        ctx.arc(centerX, centerY, radius, 0, 2 * Math.PI);
        ctx.strokeStyle = '#ff0000';
        ctx.lineWidth = 3;
        ctx.stroke();
        // 绘制内圆
        ctx.beginPath();
        ctx.arc(centerX, centerY, radius - 10, 0, 2 * Math.PI);
        ctx.lineWidth = 1;
        ctx.stroke();
        // 绘制五角星
        function drawStar(cx, cy, spikes, outerRadius, innerRadius) {
            let rot = Math.PI / 2 * 3;
            let x = cx;
            let y = cy;
            let step = Math.PI / spikes;
            ctx.beginPath();
            ctx.moveTo(cx, cy - outerRadius);
            for (let i = 0; i < spikes; i++) {
                x = cx + Math.cos(rot) * outerRadius;
                y = cy + Math.sin(rot) * outerRadius;
                ctx.lineTo(x, y);
                rot += step;
                x = cx + Math.cos(rot) * innerRadius;
                y = cy + Math.sin(rot) * innerRadius;
                ctx.lineTo(x, y);
                rot += step;
            }
            ctx.lineTo(cx, cy - outerRadius);
            ctx.closePath();
            ctx.fillStyle = '#ff0000';
            ctx.fill();
        }
        drawStar(centerX, centerY, 5, 40, 15);
        // 添加圆弧文字
        ctx.font = '24px Microsoft YaHei';
        ctx.fillStyle = '#ff0000';
        for (let i = 0; i < text.length; i++) {
            const angle = (-90 + (i * 360 / text.length)) * Math.PI / 180;
            ctx.save();
            ctx.translate(
                centerX + (radius - 30) * Math.cos(angle),
                centerY + (radius - 30) * Math.sin(angle)
            );
            ctx.rotate(angle + Math.PI/2);
            ctx.fillText(text[i], -12, 6);
            ctx.restore();
        }
        // 添加中心文字(可选)
        ctx.font = '16px Microsoft YaHei';
        ctx.fillStyle = '#ff0000';
        ctx.textAlign = 'center';
        ctx.fillText('财务专用章', centerX, centerY + 70);
    }
    // 自动生成示例
    window.onload = generateSeal;
    </script>
</body>
</html>

Python 使用 reportlab(更专业)

from reportlab.lib import colors
from reportlab.graphics import shapes
from reportlab.graphics import renderPM
from reportlab.graphics.shapes import Drawing, String, Circle, Polygon
import math
def create_seal_reportlab(text, output_path='seal.png'):
    drawing = Drawing(400, 400)
    # 外圆
    drawing.add(Circle(200, 200, 180, 
                strokeColor=colors.red, 
                strokeWidth=3, 
                fillColor=None))
    # 内圆
    drawing.add(Circle(200, 200, 160, 
                strokeColor=colors.red, 
                strokeWidth=1, 
                fillColor=None))
    # 五角星
    points = []
    for i in range(5):
        angle = -90 + i * 72
        points.append(200 + 40 * math.cos(math.radians(angle)))
        points.append(200 + 40 * math.sin(math.radians(angle)))
    drawing.add(Polygon(points, 
                strokeColor=colors.red, 
                fillColor=colors.red))
    # 圆弧文字
    for i, char in enumerate(text):
        angle = -90 + (i * 360 / len(text))
        rad = math.radians(angle)
        x = 200 + 140 * math.cos(rad)
        y = 200 + 140 * math.sin(rad)
        string = String(x, y, char, 
                       fontName='STSong-Light',
                       fontSize=28,
                       fillColor=colors.red)
        string.angle = 90 - angle
        drawing.add(string)
    # 中心文字
    drawing.add(String(200, 170, '专用章', 
                fontName='STSong-Light',
                fontSize=20,
                fillColor=colors.red,
                textAnchor='middle'))
    renderPM.drawToFile(drawing, output_path, 'PNG')
    print(f"印章已生成: {output_path}")
# 使用
create_seal_reportlab("示例科技有限公司")

安装依赖

# Python 方式
pip install Pillow reportlab
# 或使用 conda
conda install pillow reportlab

命令行工具(批处理方式)

创建 generate_seal.bat

@echo off
echo 生成电子印章
python -c "
from seal_generator import create_seal
create_seal('%1')
"

使用建议

  1. 推荐方式:使用 Python + Pillow 方式,灵活且效果好
  2. Web应用:使用 JavaScript + Canvas,适合网页集成
  3. 专业需求:使用 reportlab,支持更复杂的排版

注意事项

  • 电子印章仅供非正式用途使用
  • 重要文件请使用官方实体印章
  • 确保字体文件存在(中文字体支持)
  • 可以根据需要调整颜色、大小、文字内容

需要我详细解释某个方法或帮你定制特定样式的印章吗?

抱歉,评论功能暂时关闭!