Created
July 27, 2015 15:05
-
-
Save AndrewChamp/8e173579b312b5e3dde5 to your computer and use it in GitHub Desktop.
Edit a Microsoft Word .docx file using PHP and the zip extension.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); |
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);
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's doesn't working for
.doc
file extenstion, can you please help for.doc
file read and edit