大家好,我是Ai学习的老章

大模型生成知识卡片,我之前介绍过多次

比如我最近在用的banner

还有很多很多。。。

前文详细介绍过具体方法

今天再给大家分享一个我刚刚测试好的批量生成语录类知识卡片的方法

其实更多的是提供思路,整个过程很多细节可以再琢磨一下。

1、生成模版.svg

事实上,我生成了6个模板

  1. 默认粉紫渐变风格(default) - 柔和的粉紫色渐变背景,优雅简约

  2. 极简禅风(minimalist_zen) - 极简主义设计,注重留白和平衡

  3. 现代几何风格(modern_geometric) - 深色背景配合几何元素,现代感强

  4. 自然灵感(nature_inspired) - 自然色调和有机元素,清新自然

  5. 古典优雅(classic_elegant) - 复古纸张质感,传统典雅

  6. 科技数字风(tech_digital) - 深色背景配合科技元素,未来感

打开网易新闻 查看精彩图片

2、生成语录.json

这一步也是交给大模型,生成100条就对应100张卡片

3、批量生成脚本.py

其实就是把json中每条中英文语录、作者塞进svg模板

#quote_card_generator.py import json import re import os from pathlib import Path import argparse # 可用的模板列表 TEMPLATE_OPTIONS = {     "default": "默认粉紫渐变风格",     "minimalist_zen": "极简禅风",     "modern_geometric": "现代几何风格",     "nature_inspired": "自然灵感",     "classic_elegant": "古典优雅",     "tech_digital": "科技数字风" } def process_quote(text):     """处理输入的文本,转换为 JSON 格式"""     # 预期格式:中文名言 | English quote - 作者名 | Author name     pattern = r"(.*?)\s*\|\s*(.*?)\s*-\s*(.*?)\s*\|\s*(.*)"     match = re.match(pattern, text)          ifnot match:         raise ValueError("输入格式不正确。请使用格式:中文名言 | English quote - 作者名 | Author name")          quote_cn, quote_en, author_cn, author_en = match.groups()          return {         "quote": {             "cn": quote_cn.strip(),             "en": quote_en.strip()         },         "author": {             "cn": author_cn.strip(),             "en": author_en.strip()         }     } def list_templates():     """列出所有可用的模板"""     print("可用的引言卡片模板:")     print("-" * 40)     for key, desc in TEMPLATE_OPTIONS.items():         print(f"- {key}: {desc}")     print("-" * 40) def generate_svg(data, template_name="default"):     """根据 JSON 数据和模板名称生成 SVG"""     # 模板目录     templates_dir = Path(__file__).parent / "templates"          # 确保模板存在,否则使用默认模板     template_path = templates_dir / f"{template_name}.svg"     ifnot template_path.exists():         print(f"警告:模板 '{template_name}' 不存在,将使用默认模板。")         template_path = templates_dir / "default.svg"          # 读取 SVG 模板     with open(template_path, "r", encoding="utf-8") as f:         svg_template = f.read()          # 根据文本长度动态调整字体大小     # 中文引言字体大小 - 较长的引言会使用较小的字体     quote_cn_size = min(60, 1000 / (len(data["quote"]["cn"]) / 2 + 5))     # 英文引言字体大小     quote_en_size = min(40, 1000 / (len(data["quote"]["en"]) / 2 + 5))          # 替换模板中的占位符     svg = svg_template.replace("{{QUOTE_CN}}", data["quote"]["cn"])     svg = svg.replace("{{QUOTE_EN}}", data["quote"]["en"])     svg = svg.replace("{{AUTHOR_CN}}", data["author"]["cn"])     svg = svg.replace("{{AUTHOR_EN}}", data["author"]["en"])     svg = svg.replace("{{QUOTE_CN_SIZE}}", str(quote_cn_size))     svg = svg.replace("{{QUOTE_EN_SIZE}}", str(quote_en_size))          return svg def main():     parser = argparse.ArgumentParser(description="生成优雅的名人名言卡片")     parser.add_argument("quote", nargs="?", help="格式:中文名言 | English quote - 作者名 | Author name")     parser.add_argument("--template", "-t", default="default", help="卡片模板名称")     parser.add_argument("--output", "-o", default="quote_card.svg", help="输出文件名")     parser.add_argument("--list", "-l", action="store_true", help="列出所有可用的模板")          args = parser.parse_args()          # 如果使用了 --list 参数,列出所有模板并退出     if args.list:         list_templates()         return          # 如果没有提供引言,提示用户输入必要参数并退出     ifnot args.quote:         parser.print_help()         print("\n请提供引言参数,或使用 --list 查看可用模板。")         return          try:         # 处理输入文本         data = process_quote(args.quote)           # 生成 SVG         svg_content = generate_svg(data, args.template)                  # 保存 SVG 文件         with open(args.output, "w", encoding="utf-8") as f:             f.write(svg_content)                  print(f"成功生成 SVG 文件:{args.output}")         print(f"使用模板:{args.template}")         print("JSON 格式的数据:")         print(json.dumps(data, ensure_ascii=False, indent=2))              except Exception as e:         print(f"错误:{str(e)}") if __name__ == "__main__":     main()

使用方法:python quote_card_generator.py --template minimalist_zen --limit 5

不指定template就是用default模板,不指定limit就是全部生成

4、svg转为png

不赘述,前文讲过

方便起见,你可以把文章的脚本修改成文件夹批量执行,也可以直接在命令行中批量跑

for file in /Users/zhangbeihai/Desktop/cards/generated_cards/*.svg; do   /Users/zhangbeihai/Desktop/svg2png.sh "$file" done

制作不易,如果这篇文章觉得对你有用,可否点个关注。给我个三连击:点赞、转发和在看。若可以再给我加个,谢谢你看我的文章,我们下篇再见!