Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read and replace contents in .docx (Word) file

I need to replace content in some word documents based on User input. I am trying to read a template file (e.g. "template.docx"), and replace First name {fname}, Address {address} etc.

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}  

Is there a way to do the same in Laravel 5.3?

I am trying to do with phpword, but I can only see code to write new word files - but not read and replace existing ones. Also, when I simply read and write, the formatting is messed up.

Code:

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

$phpWord->save('b.docx');

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}
like image 507
Santosh Achari Avatar asked Dec 23 '16 06:12

Santosh Achari


People also ask

Can you find and replace in multiple Word documents?

Unfortunately, Word does not include this type of capability. Your options are either to rely on a third-party solution or write your own macro to do the changes. There are a number of third-party programs that offer the type of search-and-replace function necessary when working with multiple documents.

How do you do find and replace all in Word?

Select Find Next to see where the text appears in your file. Note: In Word for the web, select Next result to find where your text appears in a file. In the Replace with box, type the text you want. Select Replace to change the text or select Replace All to change all instances of this text in your file.

Can you edit a Word DOCX?

Click Edit Document > Edit in Word for the web to make changes to a document. When you open a document from OneDrive, Word for the web displays it in Reading view. To make changes to your document, switch to Editing view, where you can add and delete content and do other things, such as: Add tables and pictures.


1 Answers

To read and replace content from Doc file, you can use PHPWord package and download this package using composer command:

composer require phpoffice/phpword 

As per version v0.12.1, you need to require the PHP Word Autoloader.php from src/PHPWord folder and register it

require_once 'src/PhpWord/Autoloader.php';
\PhpOffice\PhpWord\Autoloader::register();

1) Open document

$template = new \PhpOffice\PhpWord\TemplateProcessor('YOURDOCPATH');

2) Replace string variables for single

$template->setValue('variableName', 'MyVariableValue');

3) Replace string variables for multi occurrence
- Clone your array placeholder to the count of your array

$template->cloneRow('arrayName', count($array));  

- Replace variable value

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

4) Save the changed document

$template->saveAs('PATHTOUPDATED.docx');

UPDATE
You can pass limit as third parameter into $template->setValue($search, $replace, $limit) to specifies how many matches should take place.

like image 179
AddWeb Solution Pvt Ltd Avatar answered Sep 20 '22 13:09

AddWeb Solution Pvt Ltd