Jump to content

How to Edit a MS Word Document


dpicella

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/182821-how-to-edit-a-ms-word-document/
Share on other sites

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.

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???

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');

"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).

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.