-
-
Save AndrewChamp/8e173579b312b5e3dde5 to your computer and use it in GitHub Desktop.
<?php | |
/** | |
* Edit a Word 2007 and newer .docx file. | |
* Utilizes the zip extension http://php.net/manual/en/book.zip.php | |
* to access the document.xml file that holds the markup language for | |
* contents and formatting of a Word document. | |
* | |
* In this example we're replacing some token strings. Using | |
* the Office Open XML standard ( https://en.wikipedia.org/wiki/Office_Open_XML ) | |
* you can add, modify, or remove content or structure of the document. | |
*/ | |
// Create the Object. | |
$zip = new ZipArchive(); | |
// Use same filename for "save" and different filename for "save as". | |
$inputFilename = 'testfile.docx'; | |
$outputFilename = 'testfile.docx'; | |
// Some new strings to put in the document. | |
$token1 = 'Hello World!'; | |
$token2 = 'Your mother smelt of elderberries, and your father was a hamster!'; | |
// Open the Microsoft Word .docx file as if it were a zip file... because it is. | |
if ($zip->open($filename, ZipArchive::CREATE)!==TRUE) { | |
echo "Cannot open $filename :( "; die; | |
} | |
// Fetch the document.xml file from the word subdirectory in the archive. | |
$xml = $zip->getFromName('word/document.xml'); | |
// Replace the tokens. | |
$xml = str_replace('{TOKEN1}', $token1, $xml); | |
$xml = str_replace('{TOKEN2}', $token2, $xml); | |
// Write back to the document and close the object | |
if ($zip->addFromString('word/document.xml', $xml)) { echo 'File written!'; } | |
else { echo 'File not written. Go back and add write permissions to this folder!l'; } | |
$zip->close(); |
Thank you so much for sharing!
Excellent work, thanks for the share!
Thank you for sharing, very concise and clear example!
Thanks, I used your code but what if their is a underscore/upper lowercase (sample_keyword) word in file than this word split and goes in two w:r in file so desire word neither be matched in file not replaced.file gets crack. so i worked in docx file . here is a link
Very nice! I also made a fork of this and turned it into a function here.
Thank you for sharing, and for images there is any way to replace image Or add images to word document using xml ?
Perfect, exactlly what I was looking for :)
It's doesn't working for .doc
file extenstion, can you please help for .doc
file read and edit
Hello, .doc Files are an old properitary Microsoft format and can't be opened with a simple unzip.
To read the content of a doc-file you can use something like https://github.com/PHPOffice/PHPWord
I want to read and update .doc
document, like i want to replace {name} with xyz
. it's posible with PHPWord library?
You can read the doc file with PHPWord and can write a docx file to save the changes.
I wouldn't recommend using and working with doc files in 2021. Just use docx instead.
When i am reading .doc
and coverting into .docx
file , file are generation but geting blank out put in docx file.
$docPath = 'abc.doc';
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$document = $phpWord->loadTemplate($docPath);
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord,'Word2007');
$docxPath = 'abc.docx';
$objWriter->save($docxPath);
Thanks for sharing!