我的计算机中有一个现有的word文档,喜欢从我的网站编辑这个文件(使用PHP)。我能找到PHPWORD
,但这只涉及新文件。我不想为整个文档编写PHP
代码,而是希望将其用于不同的内容。
有人知道任何出路吗?
答案 0 :(得分:4)
https://github.com/PHPOffice/PHPWord
PHPWord还具有一个可用于编辑现有文档的Reader。
答案 1 :(得分:3)
@nssmart,
是的,使用{value1}
等变量在word文档上标记一个部分,然后,如果已下载PHPWORD,则可以使用代码替换该部分
$PHPWord = new PHPWord();
$document = $PHPWord->loadTemplate(Doc location);
$document->setValue('value1', 'Description');
这也可用于填充表格中的数据。
答案 2 :(得分:0)
https://github.com/PHPOffice/PHPWord 我也在phpword中使用以下代码解决了这个问题
if(!file_exists('file/word.docx')){
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('demo.docx');
$templateProcessor->setValue('name', 'Akbarali');
$templateProcessor->setValue('time','13.02.2021');
$templateProcessor->setValue('month', 'January');
$templateProcessor->setValue('state','Uzbekistan');
$templateProcessor->saveAs('file/word.docx');
}
这将更改 demo.docx 文件中的单词。然后将新文件保存在 word.docx 文件夹中。 你可以在demo word文件中以${name}的形式定义变量。 即 ${name}、${time}、${month} 和 ${state}
答案 3 :(得分:0)
https://github.com/mkdreams/MDword
// New class,load template
$TemplateProcessor = new WordProcessor();
$template = 'temple.docx';
$TemplateProcessor->load($template);
// Set Value
$TemplateProcessor->setValue('value', 'r-value');
// Clone
$TemplateProcessor->clones('people', 3);
$TemplateProcessor->setValue('name#0', 'colin0');
$TemplateProcessor->setValue('name#1', [
['text'=>'colin1','style'=>'style','type'=>MDWORD_TEXT],
['text'=>1,'type'=>MDWORD_BREAK],
['text'=>'86','style'=>'style','type'=>MDWORD_TEXT]
]);
$TemplateProcessor->setValue('name#2', 'colin2');
$TemplateProcessor->setValue('sex#1', 'woman');
$TemplateProcessor->setValue('age#0', '280');
$TemplateProcessor->setValue('age#1', '281');
$TemplateProcessor->setValue('age#2', '282');
// set value for image
$TemplateProcessor->setImageValue('image', dirname(__FILE__).'/logo.jpg');
// Delete a paragraph
$TemplateProcessor->deleteP('style');
// Save
$rtemplate = __DIR__.'/r-temple.docx';
$TemplateProcessor->saveAs($rtemplate);