# document_fill **Repository Path**: yjc980121/document_fill ## Basic Information - **Project Name**: document_fill - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-08-05 - **Last Updated**: 2025-08-05 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Word文档生成器 一个强大的Word文档生成工具,支持Markdown转换和DOCX模板填充。 ## 功能特点 - 支持Markdown和DOCX互转 - 支持使用JSON数据填充DOCX/Markdown模板 - 提供命令行工具和RESTful API两种使用方式 - 支持图片、表格等复杂内容 ## 安装 ```bash # 克隆仓库 git clone https://your-repo-url/docx_generator.git cd docx_generator # 安装依赖 poetry install ``` ## 命令行工具 (CLI) ### 基本命令 ```bash # 查看帮助 poetry run python cli.py --help # 查看子命令帮助 poetry run python cli.py [command] --help ``` ### 命令示例 #### 1. Markdown 转 Word ```bash # 基本用法 poetry run python cli.py md2docx input.md -o output.docx # 指定输出文件(可选,默认为input.docx) poetry run python cli.py md2docx input.md --output custom_output.docx ``` #### 2. Word 转 Markdown ```bash # 基本用法 poetry run python cli.py docx2md input.docx -o output.md # 指定输出文件(可选,默认为input.md) poetry run python cli.py docx2md input.docx --output custom_output.md ``` #### 3. 填充模板 ```bash # 填充DOCX模板 poetry run python cli.py fill docx -t template.docx -d data.json -o output.docx # 填充Markdown模板 poetry run python cli.py fill md -t template.md -d data.json -o output.md # 自动检测模板类型(根据文件扩展名) poetry run python cli.py fill file -t template.docx -d data.json -o output.docx ``` #### 4. 查看Jinja2环境 ```bash # 查看可用的变量、过滤器和测试 poetry run python cli.py jinja-env # 使用数据文件查看可用变量 poetry run python cli.py jinja-env -d data.json ``` ## Web API ### 启动服务 ```bash poetry run python api.py ``` 服务默认运行在 `http://localhost:8000`,可以通过 `--host` 和 `--port` 参数修改。 ### API 文档 启动服务后,访问以下地址查看交互式API文档: - Swagger UI: http://localhost:8000/docs - ReDoc: http://localhost:8000/redoc ### API 端点 #### 1. 健康检查 ``` GET /health ``` **响应示例** ```json { "status": "ok" } ``` #### 2. Markdown 转 Word ``` POST /convert/md2docx ``` **参数** - `markdown`: (可选) Markdown文本内容 - `md_file`: (可选) 上传的Markdown文件 > 注意:`markdown` 和 `md_file` 参数二选一 **cURL 示例** ```bash # 使用文本内容 curl -X POST "http://localhost:8000/convert/md2docx" \ -H "accept: application/json" \ -F "markdown=# 标题\n\n这是内容" # 上传文件 curl -X POST "http://localhost:8000/convert/md2docx" \ -H "accept: application/json" \ -F "md_file=@/path/to/document.md" ``` #### 3. Word 转 Markdown ``` POST /convert/docx2md ``` **参数** - `docx_file`: (必需) 上传的Word文档 - `download`: (可选) 是否下载文件,默认为`false`,直接返回Markdown文本 **cURL 示例** ```bash # 直接返回Markdown文本 curl -X POST "http://localhost:8000/convert/docx2md" \ -H "accept: application/json" \ -F "docx_file=@/path/to/document.docx" \ -F "download=false" # 下载Markdown文件 curl -X POST "http://localhost:8000/convert/docx2md" \ -H "accept: application/json" \ -F "docx_file=@/path/to/document.docx" \ -F "download=true" \ --output output.md ``` #### 4. 填充模板 ``` POST /template/fill ``` **参数** - `template`: (必需) 上传的模板文件(支持 .docx 或 .md) - `template_data`: (必需) JSON格式的模板数据 **cURL 示例** ```bash # 填充DOCX模板 curl -X POST "http://localhost:8000/template/fill" \ -H "accept: application/json" \ -F "template=@/path/to/template.docx" \ -F "template_data='{\"name\":\"张三\",\"age\":25,\"skills\":[\"Python\",\"FastAPI\"]}'" # 填充Markdown模板 curl -X POST "http://localhost:8000/template/fill" \ -H "accept: application/json" \ -F "template=@/path/to/template.md" \ -F "template_data='{\"title\":\"示例文档\",\"content\":\"这是内容\"}'" ``` ## 模板语法 ### 基本变量 在模板中使用 `{{ variable }}` 语法插入变量: ```markdown # {{ title }} {{ content }} ``` ### 条件语句 ```markdown {% if user.is_vip %} 欢迎VIP用户:{{ user.name }} {% else %} 欢迎普通用户:{{ user.name }} {% endif %} ``` ### 循环 ```markdown ## 项目列表 {% for item in items %} - {{ item.name }} ({{ item.price }}元) {% endfor %} ``` ### 图片 在JSON数据中指定图片: ```json { "logo": { "type": "path", "data": "/path/to/image.png", "width": 200, "height": 100 } } ``` 在模板中使用: ```markdown ![Logo]({{ logo }}) ``` ## 开发 ### 运行测试 ```bash poetry run pytest ``` ### 构建Docker镜像 ```bash docker build -t docx-generator . docker run -p 8000:8000 docx-generator ``` ## 贡献 欢迎提交Issue和Pull Request。 ## 许可证 MIT