Reputation: 125
I have an existing word document in my computer and like to edit this file from my website (using PHP). I was able to find PHPWORD
but this deals with new documents only. I don't want to code PHP
for the whole document, instead wish to use it for the stuff that varies.
Does anybody know any way out?
Upvotes: 6
Views: 27645
Reputation: 904
https://github.com/PHPOffice/PHPWord I also did this problem in phpword using the following code
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');
}
This will change the words in the demo.docx file. The new file is then saved in the word.docx file folder. you can define variables in the form of $ {name} in the demo word file. that is, ${name}, ${time}, ${month} and ${state}
Upvotes: 1
Reputation: 125
@nssmart,
Yes,mark a section on your word document with a variable such as {value1}
and then, if you have PHPWORD downloaded, you can replace that section using code
$PHPWord = new PHPWord();
$document = $PHPWord->loadTemplate(Doc location);
$document->setValue('value1', 'Description');
This can be used to fill data in tables also.
Upvotes: 5
Reputation: 1455
https://github.com/PHPOffice/PHPWord
PHPWord also features a Reader which can be used to edit existing documents.
Upvotes: 6