Edit word document with php com component (Chinese is not garbled)

1. Check whether there is php_com_dotnet.dll in the ext directory of php, if not, Baidu downloads the file corresponding to the php version and put it
in 2. Configure extension=php_com_dotnet and com.allow_dcom = true in php.ini
3. Restart php
4. Start Performance

Simple editing

$file = 'C:/Users/Administrator/Desktop/11.doc';
 $word = new \com("word.application",null,CP_UTF8) or die("Unable to instantiate Word");
         // 是否显示word界面
        $word->Visible = 0;
        // 打开word文件
        $word->Documents->Open($file,true,false);
        // 获取文档内容
        $text= $word->ActiveDocument->content->Text;
        // 自己的处理逻辑
        $text = str_replace('张三','李四',$text);
        // 全选内容
        $word->ActiveDocument->Select();
        // 重新写入
        $word->Selection->TypeText($text);
        // 保存
        $word->ActiveDocument->save();
        // 关闭
        $word->Quit();
        $word = null;

Find and replace

However, the above code has a fatal flaw, that is, when you edit a formatted document, the formatting is lost after editing. If you simply replace some content, we can use the search and replace function, as follows:

$file = 'C:/Users/Administrator/Desktop/11.doc';
        $word = new \com("word.application",null,CP_UTF8) or die("Unable to instantiate Word");
        $word->Visible = 0;
        $word->Documents->Open($file,true,false);
        // 多种方式获取Find对象
        //$word->ActiveDocument->Select();
        //$Find = $word->Selection->Find;
        $Find = $word->ActiveDocument->content->Find;
        $Find->ClearFormatting();
                    //FindText, MatchCase, MatchWholeWord, MatchWildcards, MatchSoundsLike, MatchAllWordForms, Forward, Wrap, Format, ReplaceWith, Replace, MatchKashida, MatchDiacritics, MatchAlefHamza, MatchControl
        $Find->Execute('张三',false,     false,          false,          false,           false,             true,     1,true,'张三丰',2);
        $word->ActiveDocument->Save();
        $word->Quit();
        $word = null;

Edit by paragraph

 $file = 'C:/Users/Administrator/Desktop/11.doc';
        $word = new \com("word.application",null,CP_UTF8) or die("Unable to instantiate Word");
        $word->Visible = 0;
        $word->Documents->Open($file,true,false);
        $Find = $word->ActiveDocument->content->Find;
        $Find->MatchWildcards=true;
        $Find->Execute("张三");
        // 先查找一下,如果有再进行下面的循环操作
        if($Find->Found){
            $result = $Find->Parent->Text;
                $Paragraphs = $word->ActiveDocument->Paragraphs;
                for($i=1;$i<=$Paragraphs->Count;$i++){
                    $one = $Paragraphs->Item($i);
                    $Range = $one->Range;
                    // 用来判断该段落是否有需要需改的内容
                    $text = $Range->Text;
                    // 选中它 复制它的格式,修改它,再粘贴它的格式
                    if(strpos($text,'张三')!==false){
                        $Range->select();
                        $word->Selection->CopyFormat();
                        $word->Selection->TypeText('张三丰');
                        $word->Selection->PasteFormat();
                    }
                }
        }
        $word->ActiveDocument->Save();
        $word->Quit();
        $word = null;

Wildcards may be used when
searching , refer to here: Search keyword: word wildcards
https://www.officetooltips.com/word_2016/tips/using_wildcards.html

https://docs.microsoft.com/en-us/dotnet/standard/base-types/regular-expression-language-quick-reference When
I test, I can’t open the php_com_dotnet module with php7.4, and I get an error:
PHP Warning: PHP Startup: Failed to load ext\php_com_dotnet
Solution: PHP downgrade to 7.2
. Office word2007 is not installed on the computer, and the program reports an error.
Solution: just install wps

Guess you like

Origin blog.csdn.net/wang740209668/article/details/108377190