การแปลงไฟล์ Microsoft Word 2007 .docx เป็นข้อความ text ด้วย php

ตอบกระทู้

รูปแสดงอารมณ์
:icon_plusone: :like: :plusone: :gfb: :-D :) :( :-o 8O :? 8) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :!: :?: :idea: :arrow: :| :mrgreen: :angry: :baa: :biggrin:
รูปแสดงอารมณ์อื่นๆ

BBCode เปิด
[img] เปิด
[url] เปิด
[Smile icon] เปิด

กระทู้แนะนำ
   

มุมมองที่ขยายได้ กระทู้แนะนำ: การแปลงไฟล์ Microsoft Word 2007 .docx เป็นข้อความ text ด้วย php

Re: การแปลงไฟล์ Microsoft Word 2007 .docx เป็นข้อความ text ด้วย php

โดย moomike » 16/07/2014 8:49 pm

พอลองทำตาม แล้วมันขึ้นว่า Fatal error: Class 'ZipArchive' not found in C:\AppServ\www\it-technologies\open_wordTest1.php on line 27 เราจะมีวิธีแก้ยังไง ค่ะ

Re: การแปลงไฟล์ Microsoft Word 2007 .docx เป็นข้อความ text ด้วย php

โดย moomike » 16/07/2014 1:39 pm

คือว่า ลองทำตาม การแปลงไฟล์ docx เป็น text ด้วย php จากตัวอย่างด้านบน แต่มันขึ้น error ว่า Fatal error: Class 'ZipArchive' not found in C:\AppServ\www\it-technologies\open_wordTest1.php on line 27 แต่จากที่ดู ตัวที่มีปัญหา คือ $zip = new ZipArchive; แต่ว่าได้ลองดูแล้ว ตัวนี้ มันก็ยังมีอยู่ ไม่ทราบว่าเกิดจากอะไร ค่ะ

Re: การแปลงไฟล์ docx เป็น text ด้วย php

โดย tsukasaz » 07/07/2012 12:12 pm

รันโค้ด PHP
แนบไฟล์
example2.JPG
example2.JPG (97.56 KiB) Viewed 10616 times

Re: การแปลงไฟล์ docx เป็น text ด้วย php

โดย tsukasaz » 07/07/2012 12:09 pm

ทดสอบโค้ดครับ เนื้อหาในไฟล์ .docx
แนบไฟล์
example.JPG
example.JPG (62.29 KiB) Viewed 10636 times

การแปลงไฟล์ Microsoft Word 2007 .docx เป็นข้อความ text ด้วย php

โดย tsukasaz » 07/07/2012 12:08 pm

การแปลงไฟล์ docx เป็น text ด้วย php

ไฟล์ .docx เป็นไฟล์ที่สร้างจาก Microsoft Office 2007 ขึ้นไป ซึ่งมีการเปลี่ยนแปลงการกำหนดรูปแบบแฟ้มที่ใช้เป็น XML สำหรับ Microsoft Office Excel 2007, Microsoft Office Word 2007 และ Microsoft Office PowerPoint 2007 รูปแบบแฟ้มใหม่นี้เรียกว่ารูปแบบของ Office Open XML

Office Open XML เป็นชุดของรูปแบบไฟล์ที่สามารถใช้ในการแสดงเอกสารอิเล็กทรอนิกส์ รูปแบบที่กำหนดเป็นชุดคำสั่ง XML สำหรับเอกสาร สเปรดชีต การประมวลผลและนำเสนอ ที่ได้รับการผลิตโดยโปรแกรม Microsoft Office เพื่อความสะดวกในการทำงานร่วมกัน รวมถึงการใช้งานกับผู้ผลิตรายอื่น และในหลายแพลตฟอร์ม ซึ่ง Office Open XML เป็น ZIP เข้ากันได้กับแพคเกจที่มี OPC XML และแหล่งข้อมูลอื่นๆ นั่นคือสามารถดูเนื้อหาของแฟ้ม OOXML โดยการเปลี่ยนชื่อให้เป็นไฟล์ zip และเปิดด้วยเครื่องมือของ zip ไฟล์ xml นั้นจะสามารถดูได้ในเว็บเบราเซอร์หรือโปรแกรมแก้ไขข้อความธรรมดา
(ที่มา: http://en.wikipedia.org/wiki/Office_Open_XML)

สรุปสำหรับการแปลงไฟล์ docx เป็น text นั้น ให้เปลี่ยน extension จาก docx เป็น zip และเปิดมันด้วยโปรแกรม zip จะเห็นว่ามีโฟลเดอร์ word ซึ่งรวมเนื้อหาของเอกสารในรูปของ xml เพียงแค่เราเปิดไฟล์ document.xml มาอ่านในรูปรูปแบบของ XML ก็จะได้เนื้อหาของเอกสาร

สำหรับการใช้ PHP ในการอ่าน เมื่อได้ไฟล์ document.xml มาจะใช้ฟังก์ชั่น strip_tags ใน php เพื่อให้เหลือแต่ข้อความ แต่ถ้าไฟล์อยู่ในรูป zip ไม่ได้อยู่ในรูป xml ต้องใช้ไลบรารี่ของ ZipArchive ช่วยในการอ่านไฟล์

ตัวอย่างโค้ด แปลงไฟล์ docx เป็น text ด้วย PHP
(ที่มา http://www.botskool.com/geeks/how-extract-text-docx-or-odt-files-using-php)

โค้ด: เลือกทั้งหมด

<?php
 /*Name of the document file*/
$document = 'example.docx';
 
/**Function to extract text*/
function extracttext($filename) {
    //Check for extension
    $ext = end(explode('.', $filename));
 
    //if its docx file
    if($ext == 'docx')
    $dataFile = "word/document.xml";
    //else it must be odt file
    else
    $dataFile = "content.xml";    
       
    //Create a new ZIP archive object
    $zip = new ZipArchive;
 
    // Open the archive file
    if (true === $zip->open($filename)) {
        // If successful, search for the data file in the archive
        if (($index = $zip->locateName($dataFile)) !== false) {
            // Index found! Now read it to a string
            $text = $zip->getFromIndex($index);
            // Load XML from a string
            // Ignore errors and warnings
            $xml = DOMDocument::loadXML($text, LIBXML_NOENT | LIBXML_XINCLUDE | LIBXML_NOERROR | LIBXML_NOWARNING);
            // Remove XML formatting tags and return the text
            return strip_tags($xml->saveXML());
        }
        //Close the archive file
        $zip->close();
    }
 
    // In case of failure return a message
    return "File not found";
}
 
echo extracttext($document);
?>
 

ข้างบน