如何在 php 中修改 .doc 或 .docx 文件

发布于 2024-09-10 14:38:29 字数 431 浏览 7 评论 0原文

我必须在 php 中修改上传的 .doc.docx 文件。我用谷歌搜索,但我只找到了如何阅读它,但没有找到它的原样。 我想要 Word 文件原样,并将文本放在该 MS Word 文件的底部。 这怎么可能有人知道请回复。

谢谢,


我使用了以下代码:-

$w='Test';
$fp = fopen('c:/text.doc', 'a+');
fwrite($fp, $w);
fclose($fp);

它附加了字符串,但没有显示在Word文档中。 当我将 doc 文件的扩展名更改为 xml 时,它会在末尾显示字符串。 为什么它不应该显示在文档文件中。

谢谢,

I have to modify the uploaded .doc or .docx file in php. I googled but i only found how to read that but not as it is.
I want the word file as it is and put text at the bottom of that MS Word file.
How is this possible anyone know please reply.

Thanks,


I have used following code :-

$w='Test';
$fp = fopen('c:/text.doc', 'a+');
fwrite($fp, $w);
fclose($fp);

It appended the string but not showing in the word document.
When i changed the extension of doc file to xml then it shows the string at the end.
why should not it showing in doc file.

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

世界和平 2024-09-17 14:38:29

对于 Docx 文件,您可以轻松使用 zip 阅读器,例如 TbsZip 来读取和编辑主 XML 子文件(Docx 文件实际上是 zip 存档文件)。对于DOC文件来说,这是非常困难的,因为它是二进制文件。

以下是 TbsZip 的代码示例:

<?php

$x = "Hello World!";

include_once('tbszip.php');

$zip = new clsTbsZip();

// Open the document
$zip->Open('mydoc.docx');
$content = $zip->FileRead('word/document.xml');
$p = strpos($content, '</w:body>');
if ($p===false) exit("Tag </w:body> not found in document.");

// Add the text at the end
$content = substr_replace($content, '<w:p><w:r><w:t>'.$x.'</w:t></w:r></w:p>', $p, 0);
$zip->FileReplace('word/document.xml', $content, TBSZIP_STRING);

// Save as a new file
$zip->Flush(TBSZIP_FILE, 'new.docx');

For Docx files, you can easily use an zip reader such as TbsZip in order to read and edit the main XML sub-file (Docx files are actually zip archive files). For DOC files, it is very difficult because it is a binary file.

Here is an example of code with TbsZip:

<?php

$x = "Hello World!";

include_once('tbszip.php');

$zip = new clsTbsZip();

// Open the document
$zip->Open('mydoc.docx');
$content = $zip->FileRead('word/document.xml');
$p = strpos($content, '</w:body>');
if ($p===false) exit("Tag </w:body> not found in document.");

// Add the text at the end
$content = substr_replace($content, '<w:p><w:r><w:t>'.$x.'</w:t></w:r></w:p>', $p, 0);
$zip->FileReplace('word/document.xml', $content, TBSZIP_STRING);

// Save as a new file
$zip->Flush(TBSZIP_FILE, 'new.docx');
雅心素梦 2024-09-17 14:38:29

DOCX 格式的文档应该是 XML(压缩的),因此您可以尝试解析和修改它们...

请参阅 此处了解有关格式的详细信息。

Documents in the DOCX format should be XML (zipped), so you can try to parse and modify them...

See here for details about the format.

轮廓§ 2024-09-17 14:38:29

嗯,我建议将文件保存为 XML。当您在 MS Word 中编写文档时,请转到“另存为”->“另存为”。其他格式 ->此处选择 XML。

您仍然可以在 MS Word 中打开文档,并且可以轻松使用 PHP DOM 或 SimpleXML 编辑 XML。

要在底部添加一些文本,您只需向 XML 正文添加一个新的 w:p 元素:

<w:p w:rsidR="00CF175F" w:rsidRDefault="00CF175F">
<w:r>
<w:t>New text to be added</w:t>
</w:r>
</w:p>

W 是命名空间。对于 Word 2007+,情况是:

http://schemas.openxmlformats.org/wordprocessingml/2006/main

在较旧的 XML 格式中,有不同的名称空间,因此您必须查看 MS 网页才能找到正确的名称空间。

Well, I recommend saving files as XML. When you write a document in MS Word, go Save As -> Other Formats -> Choose XML here.

You will be still able to open the document in MS Word and it will be easy to edit XML with PHP DOM or SimpleXML.

To add some text at the bottom, you just need to add a new w:p element to the XML body:

<w:p w:rsidR="00CF175F" w:rsidRDefault="00CF175F">
<w:r>
<w:t>New text to be added</w:t>
</w:r>
</w:p>

W is a namespace. For Word 2007+ it is:

http://schemas.openxmlformats.org/wordprocessingml/2006/main

In the older XML format there are different namespaces so you will have to look at MS web pages to find which one is correct.

随梦而飞# 2024-09-17 14:38:29

我有相同的任务在 php 中编辑 .doc.docx 文件,我已使用此代码。

参考http://www.onlinecode。 org/update-docx-file-using-php/

    $full_path = 'template.docx';
    //Copy the Template file to the Result Directory
    copy($template_file_name, $full_path);

    // add calss Zip Archive
    $zip_val = new ZipArchive;

    //Docx file is nothing but a zip file. Open this Zip File
    if($zip_val->open($full_path) == true)
    {
        // In the Open XML Wordprocessing format content is stored.
        // In the document.xml file located in the word directory.

        $key_file_name = 'word/document.xml';
        $message = $zip_val->getFromName($key_file_name);               

        $timestamp = date('d-M-Y H:i:s');

        // this data Replace the placeholders with actual values
        $message = str_replace("client_full_name",      "onlinecode org",       $message);
        $message = str_replace("client_email_address",  "[email protected]",  $message);
        $message = str_replace("date_today",            $timestamp,             $message);      
        $message = str_replace("client_website",        "www.onlinecode.org",   $message);      
        $message = str_replace("client_mobile_number",  "+1999999999",          $message);

        //Replace the content with the new content created above.
        $zip_val->addFromString($key_file_name, $message);
        $zip_val->close();
    }

I have same task to edit .doc or .docx file in php, i have use this code for it.

Reference : http://www.onlinecode.org/update-docx-file-using-php/

    $full_path = 'template.docx';
    //Copy the Template file to the Result Directory
    copy($template_file_name, $full_path);

    // add calss Zip Archive
    $zip_val = new ZipArchive;

    //Docx file is nothing but a zip file. Open this Zip File
    if($zip_val->open($full_path) == true)
    {
        // In the Open XML Wordprocessing format content is stored.
        // In the document.xml file located in the word directory.

        $key_file_name = 'word/document.xml';
        $message = $zip_val->getFromName($key_file_name);               

        $timestamp = date('d-M-Y H:i:s');

        // this data Replace the placeholders with actual values
        $message = str_replace("client_full_name",      "onlinecode org",       $message);
        $message = str_replace("client_email_address",  "[email protected]",  $message);
        $message = str_replace("date_today",            $timestamp,             $message);      
        $message = str_replace("client_website",        "www.onlinecode.org",   $message);      
        $message = str_replace("client_mobile_number",  "+1999999999",          $message);

        //Replace the content with the new content created above.
        $zip_val->addFromString($key_file_name, $message);
        $zip_val->close();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文