开发者

Edit .doc or .docx file using php

I have to modify the uploaded .doc or .docx file in php. I googled but i only found how to read that. I want the word file as it is and put text at the bottom of that MS Word file at run开发者_运维知识库 time. How is this possible anyone know please reply or give me example script.

Thanks,


I'm the developer of PHPWord. You can use the PHPWord_Template Class to open an existing DOCX File and then replace some text marks with your individual text.

Alternatively you can open the DOCX file with the ZipArchive extension and then read/write every xml File (document.xml, header.xml, footer.xml, ...) you want. This method is nothing else than the PHPWord_Template class. ;)


You can use PHPWord.


I have same requirement for Edit .doc or .docx file using php and i have find solution for it. And i have write post on It :: http://www.onlinecode.org/update-docx-file-using-php/

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",  "ingo@onlinecode.org",  $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();
}


You only replace the predefined variables. In the following code

https://github.com/PHPOffice/PHPWord I also did this problem in phpword using the following code

 if(!file_exists('file/word.docx')){
  $templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('demo.docx');
  $templateProcessor->setValue('name', 'Akbarali');
  $templateProcessor->setValue('time','13.02.2021');
  $templateProcessor->setValue('month', 'January');
  $templateProcessor->setValue('state','Uzbekistan');
  $templateProcessor->saveAs('file/word.docx');
}

This will change the words in the demo.docx file. The new file is then saved in the word.docx file folder. you can define variables in the form of ${name} in the demo word file. that is, ${name}, ${time}, ${month} and ${state}


In this post we will show you How to update docx file using php, hear for How to update docx file using php we will give you demo and example for implement. Hear we will show you how to Edit .doc or .docx file using php. Hear we use “client_full_name”, “client_email_address”, “date_today”, “client_website”, “client_mobile_number” in .doc or .docx files

    $template_file_name = 'template.docx';
 
$rand_no = rand(111111, 999999);
$fileName = "results_" . $rand_no . ".docx";
 
$folder   = "results_";
$full_path = $folder . '/' . $fileName;
 
try
{
    if (!file_exists($folder))
    {
        mkdir($folder);
    }       
         
    //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",  "ingo@onlinecode",  $message);
        $message = str_replace("date_today",            $timestamp,             $message);      
        $message = str_replace("client_website",        "www.onlinecode",   $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();
    }
}
catch (Exception $exc) 
{
    $error_message =  "Error creating the Word Document";
    var_dump($exc);
}


Try having a look at http://www.tinybutstrong.com/ as it can create and edit Word documents, we use it in a mail merge style for generating invoices in doc and pdf.


Some of these answers dont contain the full steps so here is what ive done:

Create your word document. With in this document anything you want to replace with PHPWORD mark it like this -> ${variable_name} so for example ${value1}

$file = "docs/input/word/source.docx";
$output_file = "docs/input/word/modified.docx";

$PHPWord = new \PhpOffice\PhpWord\PhpWord();
$document = $PHPWord->loadTemplate($file);
$document->setValue('value1', 'Enter Your Text Here');
$document->saveAs($output_file);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜