phpword读取word docx文档文本及图片转html格式

最近在做一个PHP读取word文档功能,搜索一圈后决定选择用phpword第三方组件。
在这里插入图片描述

composer安装phpWord

composer require phpoffice/phpword

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

关键点

  • 对齐方式:PhpOffice\PhpWord\Style\Paragraph -> getAlignment()

  • 字体名称:\PhpOffice\PhpWord\Style\Font -> getName()

  • 字体大小:\PhpOffice\PhpWord\Style\Font -> getSize()

  • 是否加粗:\PhpOffice\PhpWord\Style\Font -> isBold()

  • 读取图片:\PhpOffice\PhpWord\Element\Image -> getImageStringData()

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

完整代码

require './vendor/autoload.php';

function docx2html($source)
{
   
   
    $phpWord = \PhpOffice\PhpWord\IOFactory::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;">'</
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天天打码

打赏买瓶护发素吧!~~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值