How can I read a Doc file with PhpWord?

夙愿已清 提交于 2019-12-07 20:26:44

问题


recently i download php word which i download from github but i dont know how to read word file via this ? there is no help in this lib. i saw a file like same as php excel bbut dont know how to use . below file: simple01 sample.php

<?php

error_reporting(E_ALL);

if(php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR'])) {
    define('EOL', PHP_EOL);
}
else {
    define('EOL', '<br />');
}

require_once '../src/PHPWord.php';

// New Word Document
echo date('H:i:s') , " Create new PHPWord object" , EOL;
$PHPWord = new PHPWord();

// New portrait section
$section = $PHPWord->createSection();

// Add text elements
$section->addText('Hello World!');
$section->addTextBreak(2);

$section->addText('I am inline styled.', array('name'=>'Verdana', 'color'=>'006699'));
$section->addTextBreak(2);

$PHPWord->addFontStyle('rStyle', array('bold'=>true, 'italic'=>true, 'size'=>16));
$PHPWord->addParagraphStyle('pStyle', array('align'=>'center', 'spaceAfter'=>100));
$section->addText('I am styled by two style definitions.', 'rStyle', 'pStyle');
$section->addText('I have only a paragraph style definition.', null, 'pStyle');

// Save File
echo date('H:i:s') , " Write to Word2007 format" , EOL;
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save(str_replace('.php', '.docx', __FILE__));

echo date('H:i:s') , " Write to OpenDocumentText format" , EOL;
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'ODText');
$objWriter->save(str_replace('.php', '.odt', __FILE__));

echo date('H:i:s') , " Write to RTF format" , EOL;
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'RTF');
$objWriter->save(str_replace('.php', '.rtf', __FILE__));


// Echo memory peak usage
echo date('H:i:s') , " Peak memory usage: " , (memory_get_peak_usage(true) / 1024 / 1024) , " MB" , EOL;

// Echo done
echo date('H:i:s') , " Done writing file" , EOL;
?>

Regards,Ahsan


回答1:


PHPWord 0.8 has provided a basic support for reading DOCX. See the code sample here.




回答2:


https://github.com/PHPOffice/PHPWord/blob/develop/composer.json

You need to src folder otherwise it is not working.you can copy src folder from https://github.com/PHPOffice/PHPWord/tree/develop/src.

<?php
try
{
    require 'vendor/autoload.php';
    $name = basename(__FILE__, '.php');
    $name="urine2";
    $source = __DIR__ . "/{$name}.docx";

    $phpWord = \PhpOffice\PhpWord\IOFactory::load($source);
    // Adding an empty Section to the document...
    $section = $phpWord->addSection();
    // Adding Text element to the Section having font styled by default...
    $section->addText($data);

    $name=basename(__FILE__, '.php');
    $source = __DIR__ . "/results/{$name}.html";

    // Saving the document as HTML file...
    $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'HTML');
    $objWriter->save($source);


}catch(Exception $e)
{
    echo $e->getLine();
    echo "<br>/";
    echo $e->getMessage();
    echo "<br/>";
    echo $e->getFile();
    exit;
}
echo "Generated successfully";


来源:https://stackoverflow.com/questions/20555057/how-can-i-read-a-doc-file-with-phpword

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!