• php 提取word 的内容 必须是docx格式


    直接上代码


    首先下载phpword插件:下面这个是我用的版本

    {
        "require": {
            "phpoffice/phpword": "^0.18.3"
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
     
    include_once "vendor/autoload.php";
    
    $info = './234.docx';
    index($info);
    function index($info)
        {
            $word  = getWord($info);
            //可以处理提取出来的数据
            foreach($word as $kkk => $vvv)
            {
               echo $vvv.'
    ';
            }
        }
    
        /**
         * 获取word文档内容
         * @param string $path
         * @return array
         */
        function getWord($path = '')
        {
    
            //加载word文档,使用phpword处理
            $phpWord = \PhpOffice\PhpWord\IOFactory::load($path);
    
            return getNodeContent($phpWord);
        }
    
        /**
         * 根据word主节点获取分节点内容
         * @param $word
         * @return array
         */
        function getNodeContent($word)
        {
            $return = [];
            //分解部分
            foreach ($word->getSections() as $section)
            {
                if ($section instanceof \PhpOffice\PhpWord\Element\Section) {
                    //分解元素
                    foreach ($section->getElements() as $element)
                    {
    
                        //文本元素
                        if ($element instanceof \PhpOffice\PhpWord\Element\TextRun) {
                            $text = '';
                            foreach ($element->getElements() as $ele) {
                                $text .= getTextNode($ele);
                            }
                            $return[] = $text;
                        }
                        //表格元素
                        else if ($element instanceof \PhpOffice\PhpWord\Element\Table) {
                            foreach ($element->getRows() as $ele)
                            {
                                $return[] = getTableNode($ele);
                            }
                        }
                    }
                }
            }
            return $return;
        }
    
        /**
         * 获取文档节点内容
         * @param $node
         * @return string
         */
        function getTextNode($node)
        {
            $return = '';
            //处理文本
            if ($node instanceof \PhpOffice\PhpWord\Element\Text)
            {
                $return .= $node->getText();
            }
            //处理图片
            else if ($node instanceof \PhpOffice\PhpWord\Element\Image)
            {
                $return .= pic2text($node);
            }
            //处理文本元素
            else if ($node instanceof \PhpOffice\PhpWord\Element\TextRun) {
                foreach ($node->getElements() as $ele) {
                    $return .= getTextNode($ele);
                }
            }
            return $return;
        }
    
        /**
         * 获取表格节点内容
         * @param $node
         * @return string
         */
        function getTableNode($node)
        {
            $return = '';
            //处理行
            if ($node instanceof \PhpOffice\PhpWord\Element\Row) {
                foreach ($node->getCells() as $ele)
                {
                    $return .= getTableNode($ele);
                }
            }
            //处理列
            else if ($node instanceof \PhpOffice\PhpWord\Element\Cell) {
                foreach ($node->getElements() as $ele)
                {
                    $return .= getTextNode($ele);
                }
            }
            return $return;
        }
    
        /**
         * 处理word文档中base64格式图片
         * @param $node
         * @return string
         */
        function pic2text($node)
        {
            //获取图片编码
            $imageData = $node->getImageStringData(true);
            //添加图片html显示标头
            $imageData = 'data:' . $node->getImageType() . ';base64,' . $imageData;
            $return = '.$imageData.'">';
            return $return;
        }
        /**
         * 处理word文档中base64格式图片
         * @param $node
         * @return string
         */
        function pic2file($node)
        {
            //图片地址(一般为word文档地址+在word中的锚点位置)
            $imageSrc  = 'images/' . md5($node->getSource()) . '.' . $node->getImageExtension();
            $imageData = $node->getImageStringData(true);
            //将图片保存在本地
            file_put_contents($imageSrc, base64_decode($imageData));
            return $imageSrc;
        }
    
        /**
         * 将word转化为html(转换存储html文件后展示)
         * @param $path
         * @throws \PhpOffice\PhpWord\Exception\Exception
         */
        function word2html($path)
        {
            $phpWord = FileImportService::getOne($path);
            //转为html处理
            $xmlWriter  = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, "HTML");
            $path = pathinfo($path);
            $fileName = $path['dirname'] . '/' . $path['filename'] . '.html';
            $xmlWriter->save($fileName);
            $html = file_get_contents($fileName);
            echo $html;
            die;
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
  • 相关阅读:
    心理软件使用问题三
    puttygen工具ppk文件版本配置
    Android11.0默认将所有app动态申请的权限打开
    计算机毕业设计Java携手同游旅游社交平台(源码+系统+mysql数据库+Lw文档)
    Linux命令之压缩zip
    VQA的应用(调研)
    TMUX终端复用工具小解
    白鹭群优化算法(ESOA)附matlab代码
    【大规模 MIMO 检测】基于ADMM的大型MU-MIMO无穷大范数检测研究(Matlab代码实现)
    【深度学习】特征融合的重要方法 | 张量的拼接 | torch.cat()函数 | torch.add(函数
  • 原文地址:https://blog.csdn.net/weixin_39616995/article/details/126367870