有搞过导入word文档的大佬吗

比如我打开word

有搞过导入word文档的大佬吗
需要获取这个标题

然后内容就类似于富文本一样,图片啥的在一起

《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
最佳答案

已解决

$phpWord = IOFactory::load($request->file('word'));

        $htmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, "HTML");
        $content = '';
        foreach ($phpWord->getSections() as $section) {
            $writer = new \PhpOffice\PhpWord\Writer\HTML\Element\Container($htmlWriter, $section);
            $content .= $writer->write();
        }


        $title = '';
        $sections = $phpWord->getSections();
        foreach ($sections as $section) {
            $elements = $section->getElements();
            foreach ($elements as $element) {
                if ($element instanceof \PhpOffice\PhpWord\Element\Text) {
                    if ($title) break;
                    $title = $element->getText();
                } elseif ($element instanceof \PhpOffice\PhpWord\Element\TextRun) {
                    foreach ($element->getElements() as $textElement) {
                        if ($textElement instanceof \PhpOffice\PhpWord\Element\Text) {
                            if ($title) break;
                            $title = $textElement->getText();
                        }
                    }
                }
            }
        }

        return app('show')->success([
            'title' => $title,
            'content' => $content
        ]);
1年前 评论
讨论数量: 6

简单的用上面朋友留言的就行,复杂的.docx 转 .zip 解压读取xml,如果是doc先转成docx,
还有些简单的工具,可以exec调用,比如说 LibreOfficePandoc

1年前 评论

通过phpoffice/phpword读取Word 文档,通过遍历文档的段落和元素获取标题。

1年前 评论
猪猪

以前做过类似的项目,具体我忘了是上传word文件 还是复制word内容来实现发布文章。晚上我回去给你翻翻,当初这个问题好像还卡了我很久

1年前 评论
sanders

:joy: 十四年前我们是把word转成html之后用匹配标签的方式搞的。

1年前 评论

已解决

$phpWord = IOFactory::load($request->file('word'));

        $htmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, "HTML");
        $content = '';
        foreach ($phpWord->getSections() as $section) {
            $writer = new \PhpOffice\PhpWord\Writer\HTML\Element\Container($htmlWriter, $section);
            $content .= $writer->write();
        }


        $title = '';
        $sections = $phpWord->getSections();
        foreach ($sections as $section) {
            $elements = $section->getElements();
            foreach ($elements as $element) {
                if ($element instanceof \PhpOffice\PhpWord\Element\Text) {
                    if ($title) break;
                    $title = $element->getText();
                } elseif ($element instanceof \PhpOffice\PhpWord\Element\TextRun) {
                    foreach ($element->getElements() as $textElement) {
                        if ($textElement instanceof \PhpOffice\PhpWord\Element\Text) {
                            if ($title) break;
                            $title = $textElement->getText();
                        }
                    }
                }
            }
        }

        return app('show')->success([
            'title' => $title,
            'content' => $content
        ]);
1年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!