Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I read write edit pptx/docx/xlsx files using PHP?

Is there a library extension available for efficiently handling pptx/docx/xlsx files using PHP? As of now I am more interested in PPTX files.

like image 454
Kumar Avatar asked Apr 06 '11 06:04

Kumar


4 Answers

As per what i know, those file formats docx,xlsx,pptx are just zip files. they belong to Office Open XML (OOXML) standard.

In PHP we have this library for manipulating this type of zip documents: http://php.net/manual/en/book.zip.php

You can find all documentation about this ooxml standard here: http://www.ecma-international.org/publications/standards/Ecma-376.htm

The best way to test the structure of these ooxml file is to change the file extension to .zip, and the extract it out to find out what are inside.

If you don't want to build your own library for processing ooxml files, you can refer to a related question here for more info: PHP OOXML Libraries?

As i read from the related stackoverflow question mentioned above, you can use the phpdocx, or somewhat other called PHPWord.

Hope this may clarify some steps to help get your wants done

like image 139
nicola Avatar answered Oct 04 '22 21:10

nicola


There is no one library that can handle all three formats, but there are individual libraries that can read and/or write the individual formats.

  • PHPPowerpoint can write, but not read, pptx files
  • PHPWord can write, but not read, docx files
  • PHPLiveDocx can write (and I believe also reads) docx files
  • PHPExcel can read and write xlsx files
like image 30
Mark Baker Avatar answered Oct 04 '22 20:10

Mark Baker


here is the working example that can read only docx file.

<?php
    $filename = "file.docx"; // or /var/www/html/file.docx 

    $content = read_file_docx($filename); 
    if($content !== false) {
        echo nl2br($content); 
    }  
    else {
        echo 'Couldn\'t the file. Please check that file.'; 
    }

    function read_file_docx($filename){ 

        $striped_content = ''; 
        $content = ''; 

        if(!$filename || !file_exists($filename)) return false;  

        $zip = zip_open($filename);

        if (!$zip || is_numeric($zip)) return false; 

        while ($zip_entry = zip_read($zip)) { 

            if (zip_entry_open($zip, $zip_entry) == FALSE) continue; 

            if (zip_entry_name($zip_entry) != "word/document.xml") continue; 

            $content .= zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)); 

            zip_entry_close($zip_entry); 
        }// end while 

        zip_close($zip); 

        //echo $content; 

        //file_put_contents('1.xml', $content);  

        $content = str_replace('</w:r></w:p></w:tc><w:tc>', " ", $content); 
        $content = str_replace('</w:r></w:p>', "\r\n", $content); 
        $striped_content = strip_tags($content); 

        return $striped_content; 
    }
?> 
like image 32
Manoj Prajapat Avatar answered Oct 04 '22 22:10

Manoj Prajapat


You can build docx/xlsx/pptx documents from templates using the OpenTBS PHP tool.

The version currently in development will improve the support of XLSX.

like image 34
Skrol29 Avatar answered Oct 04 '22 21:10

Skrol29