dpicella Posted November 24, 2009 Share Posted November 24, 2009 I want to add some text to the end of some Microsoft Word documents. Is there a way to do this? I'm on a Linux server and therefore can't use COM to open an instance of a Word application. Basically, this would be like inserting some text such as a time stamp at the end of the .doc file. If php can't do this, what about Perl? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/182821-how-to-edit-a-ms-word-document/ Share on other sites More sharing options...
Goldeneye Posted November 24, 2009 Share Posted November 24, 2009 I don't know exactly how to edit a Microsoft Word document through PHP, but I can outline how it might be done. <?php $str = 'The text to append to the Microsoft Word Document'; $file_handle = fopen('/path/to/MS_Word_document.doc', a); fwrite($file_handle, $str); fclose($file_handle); ?> I'm not sure that would work, but it should be a push in the right direction. Quote Link to comment https://forums.phpfreaks.com/topic/182821-how-to-edit-a-ms-word-document/#findComment-964947 Share on other sites More sharing options...
dpicella Posted November 24, 2009 Author Share Posted November 24, 2009 Thanks! Well... that did not work. I did notice that the text does get added at the end of the file stream but it does not show up in word. The word file seems to be encoded in binary format and not ascii. Perhaps there is a Perl script that can edit a word file??? Quote Link to comment https://forums.phpfreaks.com/topic/182821-how-to-edit-a-ms-word-document/#findComment-964955 Share on other sites More sharing options...
Goldeneye Posted November 24, 2009 Share Posted November 24, 2009 Thanks! Well... that did not work. I did notice that the text does get added at the end of the file stream but it does not show up in word. The word file seems to be encoded in binary format and not ascii. Perhaps there is a Perl script that can edit a word file??? In that case... Try $file_handle = fopen('/path/to/MS_Word_document.doc', 'ab'); instead of $file_handle = fopen('/path/to/MS_Word_document.doc', 'a'); Quote Link to comment https://forums.phpfreaks.com/topic/182821-how-to-edit-a-ms-word-document/#findComment-964983 Share on other sites More sharing options...
chaiwei Posted November 25, 2009 Share Posted November 25, 2009 "ab" means write the file in potability mode? what difference with "t"? Quote Link to comment https://forums.phpfreaks.com/topic/182821-how-to-edit-a-ms-word-document/#findComment-965208 Share on other sites More sharing options...
Goldeneye Posted November 25, 2009 Share Posted November 25, 2009 "ab" means write the file in potability mode? what difference with "t"? "b" forces binary-mode for the opened-file. "t" has to do with line endings. On Windows, using "t" will cause PHP to translate "\n" (the *nix linebreak) to "\r\n" (the Windows linebreak). Quote Link to comment https://forums.phpfreaks.com/topic/182821-how-to-edit-a-ms-word-document/#findComment-965213 Share on other sites More sharing options...
l0ve2hat3 Posted November 25, 2009 Share Posted November 25, 2009 if you can save the word doc as an xml file then you can edit it. Quote Link to comment https://forums.phpfreaks.com/topic/182821-how-to-edit-a-ms-word-document/#findComment-965217 Share on other sites More sharing options...
dpicella Posted November 25, 2009 Author Share Posted November 25, 2009 In that case... Try $file_handle = fopen('/path/to/MS_Word_document.doc', 'ab'); instead of $file_handle = fopen('/path/to/MS_Word_document.doc', 'a'); Unfortunately this does not work. Quote Link to comment https://forums.phpfreaks.com/topic/182821-how-to-edit-a-ms-word-document/#findComment-965304 Share on other sites More sharing options...
dpicella Posted November 25, 2009 Author Share Posted November 25, 2009 I found this: http://www.php.net/manual/en/function.fwrite.php#53622 Which I also think is close. If one wanted to find and replace some text I think you could use pack/unpack to edit a section of text but it would still not be the same as editing the file and appending some text at the end. Quote Link to comment https://forums.phpfreaks.com/topic/182821-how-to-edit-a-ms-word-document/#findComment-965305 Share on other sites More sharing options...
dpicella Posted November 25, 2009 Author Share Posted November 25, 2009 if you can save the word doc as an xml file then you can edit it. I think this is the way to go. I am going to see if we can get our documents in the newer Open XML format. Quote Link to comment https://forums.phpfreaks.com/topic/182821-how-to-edit-a-ms-word-document/#findComment-965306 Share on other sites More sharing options...
dgoosens Posted November 25, 2009 Share Posted November 25, 2009 hi, I am not sure... but I think PHPLIVEDOCX will be able to help you out: http://www.phplivedocx.org/2009/02/03/generate-pdf-docx-doc-and-rtf-files-with-php/ otherwise, go for the ODT format (OpenOffice) and use odtPHP http://www.odtphp.com/ Quote Link to comment https://forums.phpfreaks.com/topic/182821-how-to-edit-a-ms-word-document/#findComment-965308 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.