zoukankan      html  css  js  c++  java
  • PHP读取office word文档内容及图片

    PHP读取word文档里的文字及图片,并保存

    一、composer安装phpWord

    composer require phpoffice/phpword

    传送门:https://packagist.org/packages/phpoffice/phpword

    二、phpWord 读取 docx 文档(注意是docx格式,doc格式不行

    如果你的文件是doc格式,直接另存为一个docx就行了;如果你的doc文档较多,可以下一个批量转换工具:http://www.batchwork.com/en/doc2doc/download.htm

    如果你还没配置自动加载,则先配置一下:

    require './vendor/autoload.php';

    加载文档:

    $dir = str_replace('\', '/', __DIR__) . '/';
    $source = $dir . 'test.docx';
    $phpWord = PhpOfficePhpWordIOFactory::load($source);

    三、关键点

    1)对齐方式:PhpOfficePhpWordStyleParagraph -> getAlignment()

    2)字体名称:PhpOfficePhpWordStyleFont -> getName()

    3)字体大小:PhpOfficePhpWordStyleFont -> getSize()

    4)是否加粗:PhpOfficePhpWordStyleFont -> isBold()

    5)读取图片:PhpOfficePhpWordElementImage -> getImageStringData()

    6)ba64格式图片数据保存为图片:file_put_contents($imageSrc, base64_decode($imageData))

    四、完整代码

    require './vendor/autoload.php';
    
    function docx2html($source)
    {
        $phpWord = PhpOfficePhpWordIOFactory::load($source);
        $html = '';
        foreach ($phpWord->getSections() as $section) {
            foreach ($section->getElements() as $ele1) {
                $paragraphStyle = $ele1->getParagraphStyle();
                if ($paragraphStyle) {
                    $html .= '<p style="text-align:'. $paragraphStyle->getAlignment() .';text-indent:20px;">';
                } else {
                    $html .= '<p>';
                }
                if ($ele1 instanceof PhpOfficePhpWordElementTextRun) {
                    foreach ($ele1->getElements() as $ele2) {
                        if ($ele2 instanceof PhpOfficePhpWordElementText) {
                            $style = $ele2->getFontStyle();
                            $fontFamily = mb_convert_encoding($style->getName(), 'GBK', 'UTF-8');
                            $fontSize = $style->getSize();
                            $isBold = $style->isBold();
                            $styleString = '';
                            $fontFamily && $styleString .= "font-family:{$fontFamily};";
                            $fontSize && $styleString .= "font-size:{$fontSize}px;";
                            $isBold && $styleString .= "font-weight:bold;";
                            $html .= sprintf('<span style="%s">%s</span>',
                                $styleString,
                                mb_convert_encoding($ele2->getText(), 'GBK', 'UTF-8')
                            );
                        } elseif ($ele2 instanceof PhpOfficePhpWordElementImage) {
                            $imageSrc = 'images/' . md5($ele2->getSource()) . '.' . $ele2->getImageExtension();
                            $imageData = $ele2->getImageStringData(true);
                            // $imageData = 'data:' . $ele2->getImageType() . ';base64,' . $imageData;
                            file_put_contents($imageSrc, base64_decode($imageData));
                            $html .= '<img src="'. $imageSrc .'" style="100%;height:auto">';
                        }
                    }
                }
                $html .= '</p>';
            }
        }
    
        return mb_convert_encoding($html, 'UTF-8', 'GBK');
    }
    
    
    
    $dir = str_replace('\', '/', __DIR__) . '/';
    $source = $dir . 'test.docx';
    echo docx2html($source);

    五、补充

    很明显,这是一个简陋的word读取示例,只读取了段落的对齐方式,文字的字体、大小、是否加粗及图片等信息,其他例如文字颜色、行高。。。等等信息都忽悠了。需要的话,请自行查看phpWord源码,看PhpOfficePhpWordStylexxx 和 PhpOfficePhpWordElementxxx 等类里有什么读取方法就可以了

  • 相关阅读:
    Python循环语句
    Python判断语句
    MySQL的基本操作汇总
    Python函数、类
    Python字典、集合
    Python列表、元组
    python数据类型--数字、字符串
    Python基础认识
    搭建Python独立虚拟环境
    Python包管理工具
  • 原文地址:https://www.cnblogs.com/tujia/p/12133615.html
Copyright © 2011-2022 走看看