Skip to content

Instantly share code, notes, and snippets.

@AndrewChamp
Created July 27, 2015 15:05
Show Gist options
  • Save AndrewChamp/8e173579b312b5e3dde5 to your computer and use it in GitHub Desktop.
Save AndrewChamp/8e173579b312b5e3dde5 to your computer and use it in GitHub Desktop.
Edit a Microsoft Word .docx file using PHP and the zip extension.
<?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();
@matrunchyk
Copy link

Thanks for sharing!

Copy link

ghost commented Jul 3, 2017

Thank you so much for sharing!

@Zei33
Copy link

Zei33 commented Jul 20, 2017

Excellent work, thanks for the share!

@cagp-dev-mtl
Copy link

Thank you for sharing, very concise and clear example!

@Johnykaushik
Copy link

Johnykaushik commented Jan 27, 2019

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

@abarke
Copy link

abarke commented Jun 13, 2019

Very nice! I also made a fork of this and turned it into a function here.

@ChinigamiHunter
Copy link

Thank you for sharing, and for images there is any way to replace image Or add images to word document using xml ?

@hebinet
Copy link

hebinet commented Mar 12, 2020

Perfect, exactlly what I was looking for :)

@JBavaliya
Copy link

JBavaliya commented Apr 28, 2021

It's doesn't working for .doc file extenstion, can you please help for .doc file read and edit

@hebinet
Copy link

hebinet commented Apr 28, 2021

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

@JBavaliya
Copy link

@hebinet

I want to read and update .doc document, like i want to replace {name} with xyz. it's posible with PHPWord library?

@hebinet
Copy link

hebinet commented Apr 28, 2021

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.

@JBavaliya
Copy link

JBavaliya commented Apr 28, 2021

@hebinet

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