安装Phpword
composer require phpoffice/phpword
使用
<?php
require_once 'vendor/autoload.php';
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\IOFactory;
use PhpOffice\PhpWord\TemplateProcessor;
// 加载模板文件
$templatePath = 'path/to/your/template.docx';
$templateProcessor = new TemplateProcessor($templatePath);
// 替换模板中的变量(例如:{name})
$templateProcessor->setValue('name', '张三');
$templateProcessor->setValue('date', date('Y-m-d'));
// 保存新文档
$newDocPath = 'path/to/your/new_document.docx';
$templateProcessor->saveAs($newDocPath);
echo "文档已生成在: " . $newDocPath;