读取并替换.docx(Word)文件中的内容

San*_*ari 6 php ms-word docx laravel laravel-5.3

我需要根据用户输入替换某些Word文档中的内容。我正在尝试读取模板文件(例如“ template.docx”),并替换名字{fname},地址{address}等。

template.docx:

To,
The Office,
{officeaddress}
Sub:  Authorization Letter
Sir / Madam,

I/We hereby authorize  to  {Ename}  whose signature is attested here below, to submit application and collect Residential permit for {name}  
Kindly allow him to support our International assignee

{name}                                          {Ename}  
Run Code Online (Sandbox Code Playgroud)

Laravel 5.3中有没有办法做到这一点?

我正在尝试使用phpword,但是我只能看到编写新的word文件的代码-但不能读取和替换现有的word文件。另外,当我只是读和写时,格式被弄乱了。

码:

$file = public_path('template.docx');
$phpWord = \PhpOffice\PhpWord\IOFactory::load($file);

$phpWord->save('b.docx');
Run Code Online (Sandbox Code Playgroud)

b.docx

To,
The Office,
{officeaddress}

Sub: 
 Authorization Letter
Sir / Madam,


I/We hereby authorize 
 to

{Ename}


whose signature is attested here below, to submit a
pplication and collect Residential permit
 for 
{name}

Kindly allow him to support our International assignee


{name}













{
E
name}
Run Code Online (Sandbox Code Playgroud)

San*_*ari 7

这是@ addweb-solution-pvt-ltd的答案的工作版本。

//This is the main document in  Template.docx file.
$file = public_path('template.docx');

$phpword = new \PhpOffice\PhpWord\TemplateProcessor($file);

$phpword->setValue('{name}','Santosh');
$phpword->setValue('{lastname}','Achari');
$phpword->setValue('{officeAddress}','Yahoo');

$phpword->saveAs('edited.docx');
Run Code Online (Sandbox Code Playgroud)

但是,并非所有{name}字段都在更改。不知道为什么。


或者:

// Creating the new document...
$zip = new \PhpOffice\PhpWord\Shared\ZipArchive();

//This is the main document in a .docx file.
$fileToModify = 'word/document.xml';

$file = public_path('template.docx');
$temp_file = storage_path('/app/'.date('Ymdhis').'.docx');
copy($template,$temp_file);

if ($zip->open($temp_file) === TRUE) {
    //Read contents into memory
    $oldContents = $zip->getFromName($fileToModify);

    echo $oldContents;

    //Modify contents:
    $newContents = str_replace('{officeaddqress}', 'Yahoo \n World', $oldContents);
    $newContents = str_replace('{name}', 'Santosh Achari', $newContents);

    //Delete the old...
    $zip->deleteName($fileToModify);
    //Write the new...
    $zip->addFromString($fileToModify, $newContents);
    //And write back to the filesystem.
    $return =$zip->close();
    If ($return==TRUE){
        echo "Success!";
    }
} else {
    echo 'failed';
}
Run Code Online (Sandbox Code Playgroud)

效果很好。仍在尝试弄清楚如何将其另存为新文件并强制下载。


Add*_*Ltd 2

要读取和替换 Doc 文件中的内容,您可以使用PHPWord包并使用Composer命令下载此包:

composer require phpoffice/phpword 
Run Code Online (Sandbox Code Playgroud)

根据版本 v0.12.1,您需要Autoloader.phpsrc/PHPWord文件夹中获取 PHP Word 并注册它

require_once 'src/PhpWord/Autoloader.php';
\PhpOffice\PhpWord\Autoloader::register();
Run Code Online (Sandbox Code Playgroud)

1)打开文档

$template = new \PhpOffice\PhpWord\TemplateProcessor('YOURDOCPATH');
Run Code Online (Sandbox Code Playgroud)

2)将字符串变量替换为single

$template->setValue('variableName', 'MyVariableValue');
Run Code Online (Sandbox Code Playgroud)

3) 替换多次出现的字符串变量
- 将数组占位符克隆到数组的计数

$template->cloneRow('arrayName', count($array));  
Run Code Online (Sandbox Code Playgroud)

- 替换变量值

for($number = 0; $number < count($array); $number++) {
    $template->setValue('arrayName#'.($number+1), htmlspecialchars($array[$number], ENT_COMPAT, 'UTF-8'));
}
Run Code Online (Sandbox Code Playgroud)

4)保存修改后的文档

$template->saveAs('PATHTOUPDATED.docx');
Run Code Online (Sandbox Code Playgroud)

更新
您可以将 limit 作为第三个参数传递给$template->setValue($search, $replace, $limit)指定应发生多少次匹配。