Home  >  Article  >  Backend Development  >  Use PHP to change the way of reading WORD content

Use PHP to change the way of reading WORD content

藏色散人
藏色散人forward
2021-08-27 16:48:324812browse

Use PHP to change the way of reading WORD content

Project: Questionnaire

Requirement: WORD import questionnaire

Background: There are hundreds of WORD format questionnaires in the operation. If you go to the backend to enter them manually, There is no doubt a lot of work, and I hope it can be imported directly.

Mood: After receiving the request, I had mixed feelings, because I have done excel import before, and there are ready-made plug-ins, and I have to search a lot of codes.

Word import undoubtedly involves knowledge blind spots, but the demand is there, and you can’t beat the product classmates! I just had to bite the bullet.

Difficulty: Word is difficult to read the content, and the content is not structured well when read.

Ideas to solve the problem:

Read the WORD first, and then talk about how to structure it.

Read WORD:

At first I thought about using PHPWORD. After all, a mature plug-in like PHPOFFICE should be able to read WORD content directly.

However, the reality is very ugly. I searched all the documents and could not find a way to directly read the WORD content. PHPWORD only provides methods to convert WORD into HTML and TDF.

Conversion idea:

Since I can’t read WORD, then I can read HTML. I just need to convert WORD into HTML, and then read the HTML content. That’s it.

Code:

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use PhpOffice\PhpSpreadsheet\Reader\Html;
use PhpOffice\PhpWord\Reader\Word2007;
class Test extends Command {
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = &#39;word&#39;;
    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = &#39;word&#39;;
    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct() {
        parent::__construct();
    }
    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle(Word2007 $word) {
        //WORD转换HTML
        $result=$word->load(storage_path(&#39;测试.docx&#39;));
        $write=new \PhpOffice\PhpWord\Writer\HTML($result);
        $write->save(storage_path().&#39;/测试.html&#39;);
        //读取HTML内容
        $document=new \DOMDocument();
        $document->loadHTML(file_get_contents(storage_path(&#39;测试.html&#39;)));
        $html=simplexml_import_dom($document);
        dd((array)$html->body);
    }
}

Start test: New test.docx

Test.docx Content:

Use PHP to change the way of reading WORD content

Execute script:

php artisan word

Result:

Use PHP to change the way of reading WORD content

The above is the detailed content of Use PHP to change the way of reading WORD content. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete