PHPでPPTファイルを読む方法

WBOY
リリース: 2016-07-29 09:10:22
オリジナル
1619 人が閲覧しました

この記事の例では、PHP で PPT ファイルを読み取る方法を説明します。詳細は次のとおりです。

最近、Baidu や Docin などの Web サイトで PPT を表示する必要があるものを作成しました。Web サイトで直接読んだり、オンラインで検索したりできます。長い間検索しても何も見つかりませんでした。ソフトウェアのインストールなどに関するものばかりです。PHP Web サイトがそのスペースに配置されている場合、ソフトウェアをインストールする場所を誰が教えてくれるのでしょうか。ナンセンスなことを言っていませんか?幸いなことに、最終的に解決策を見つけたのは、PPT を操作するための PHP クラスです。この Web サイトには、OFFICES ソフトウェアを操作するための他のクラスも提供されています。ここで、URL を掲載します。すべての人に捧げます: http://phppowerpoint.codeplex.com/。ここをクリックしてこのサイトからダウンロードすることもできます。

このクラスはPPT2007形式の読み込み・書き込み・生成を実現できます

その時に見たサンプルも以下に載せておきます

このサンプルではphppowerpointクラスを使用しています。 1 つはパラメーターの意味を理解すること、もう 1 つは文字に注意を払うことです。コレクション

<&#63;php
header("content-type:text/html;charset=utf-8");//字体设置防止乱码
error_reporting(E_ALL);
/** Include path **/
set_include_path(get_include_path() . PATH_SEPARATOR . '../Classes/');
/** PHPPowerPoint */
include 'PHPPowerPoint.php';
/** PHPPowerPoint_IOFactory */
include 'PHPPowerPoint/IOFactory.php';
// Create new PHPPowerPoint object
//echo date('H:i:s') . " Create new PHPPowerPoint object\n";
$objPHPPowerPoint = new PHPPowerPoint();
$objPHPPowerPoint->getProperties()->setCreator("Maarten Balliauw");
$objPHPPowerPoint->getProperties()->setLastModifiedBy("Maarten Balliauw");
$objPHPPowerPoint->getProperties()->setTitle("Office 2007 PPTX Test Document");
$objPHPPowerPoint->getProperties()->setSubject("Office 2007 PPTX Test Document");
$objPHPPowerPoint->getProperties()->setDescription("Test document for Office 2007 PPTX, generated using PHP classes.");
$objPHPPowerPoint->getProperties()->setKeywords("office 2007 openxml php");
$objPHPPowerPoint->getProperties()->setCategory("Test result file");
// Remove first slide
//echo date('H:i:s') . " Remove first slide\n";
$objPHPPowerPoint->removeSlideByIndex(0);
// Create templated slide
//echo date('H:i:s') . " Create templated slide\n";
/*$currentSlide = createTemplatedSlide($objPHPPowerPoint); // local function
// Create a shape (text)
echo date('H:i:s') . " Create a shape (rich text)\n";
$shape = $currentSlide->createRichTextShape();
$shape->setHeight(200);
$shape->setWidth(600);
$shape->setOffsetX(10);
$shape->setOffsetY(400);
$shape->getAlignment()->setHorizontal( PHPPowerPoint_Style_Alignment::HORIZONTAL_LEFT );
$textRun = $shape->createTextRun('Introduction to');
$textRun->getFont()->setBold(true);
$textRun->getFont()->setSize(28);
$textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) );
$shape->createBreak();
$textRun = $shape->createTextRun('PHPPowerPoint');
$textRun->getFont()->setBold(true);
$textRun->getFont()->setSize(60);
$textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) );
// Create templated slide
echo date('H:i:s') . " Create templated slide\n";
$currentSlide = createTemplatedSlide($objPHPPowerPoint); // local function
// Create a shape (text)
echo date('H:i:s') . " Create a shape (rich text)\n";
$shape = $currentSlide->createRichTextShape();
$shape->setHeight(100);
$shape->setWidth(930);
$shape->setOffsetX(10);
$shape->setOffsetY(10);
$shape->getAlignment()->setHorizontal( PHPPowerPoint_Style_Alignment::HORIZONTAL_LEFT );
$textRun = $shape->createTextRun('What is PHPPowerPoint?');
$textRun->getFont()->setBold(true);
$textRun->getFont()->setSize(48);
$textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) );
// Create a shape (text)
echo date('H:i:s') . " Create a shape (rich text)\n";
$shape = $currentSlide->createRichTextShape();
$shape->setHeight(600);
$shape->setWidth(930);
$shape->setOffsetX(10);
$shape->setOffsetY(100);
$shape->getAlignment()->setHorizontal( PHPPowerPoint_Style_Alignment::HORIZONTAL_LEFT );
$textRun = $shape->createTextRun('- A class library');
$textRun->getFont()->setSize(36);
$textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) );
$shape->createBreak();
$textRun = $shape->createTextRun('- Written in PHP');
$textRun->getFont()->setSize(36);
$textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) );
$shape->createBreak();
$textRun = $shape->createTextRun('- Representing a presentation');
$textRun->getFont()->setSize(36);
$textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) );
$shape->createBreak();
$textRun = $shape->createTextRun('- Supports writing to different file formats');
$textRun->getFont()->setSize(36);
$textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) );
// Create templated slide
echo date('H:i:s') . " Create templated slide\n";
$currentSlide = createTemplatedSlide($objPHPPowerPoint); // local function
// Create a shape (text)
echo date('H:i:s') . " Create a shape (rich text)\n";
$shape = $currentSlide->createRichTextShape();
$shape->setHeight(100);
$shape->setWidth(930);
$shape->setOffsetX(10);
$shape->setOffsetY(10);
$shape->getAlignment()->setHorizontal( PHPPowerPoint_Style_Alignment::HORIZONTAL_LEFT );
$textRun = $shape->createTextRun('What\'s the point?');
$textRun->getFont()->setBold(true);
$textRun->getFont()->setSize(48);
$textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) );
// Create a shape (text)
echo date('H:i:s') . " Create a shape (rich text)\n";
$shape = $currentSlide->createRichTextShape();
$shape->setHeight(600);
$shape->setWidth(930);
$shape->setOffsetX(10);
$shape->setOffsetY(100);
$shape->getAlignment()->setHorizontal( PHPPowerPoint_Style_Alignment::HORIZONTAL_LEFT );
$textRun = $shape->createTextRun('- Generate slide decks');
$textRun->getFont()->setSize(36);
$textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) );
$shape->createBreak();
$textRun = $shape->createTextRun('  - Represent business data');
$textRun->getFont()->setSize(28);
$textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) );
$shape->createBreak();
$textRun = $shape->createTextRun('  - Show a family slide show');
$textRun->getFont()->setSize(28);
$textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) );
$shape->createBreak();
$textRun = $shape->createTextRun('  - ...');
$textRun->getFont()->setSize(28);
$textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) );
$shape->createBreak();
$textRun = $shape->createTextRun('- Export these to different formats');
$textRun->getFont()->setSize(36);
$textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) );
$shape->createBreak();
$textRun = $shape->createTextRun('  - PowerPoint 2007');
$textRun->getFont()->setSize(28);
$textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) );
$shape->createBreak();
$textRun = $shape->createTextRun('  - Serialized');
$textRun->getFont()->setSize(28);
$textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) );
$shape->createBreak();
$textRun = $shape->createTextRun('  - ... (more to come) ...');
$textRun->getFont()->setSize(28);
$textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) );
// Create templated slide
echo date('H:i:s') . " Create templated slide\n";
$currentSlide = createTemplatedSlide($objPHPPowerPoint); // local function
// Create a shape (text)
echo date('H:i:s') . " Create a shape (rich text)\n";
$shape = $currentSlide->createRichTextShape();
$shape->setHeight(100);
$shape->setWidth(930);
$shape->setOffsetX(10);
$shape->setOffsetY(10);
$shape->getAlignment()->setHorizontal( PHPPowerPoint_Style_Alignment::HORIZONTAL_LEFT );
$textRun = $shape->createTextRun('Need more info?');
$textRun->getFont()->setBold(true);
$textRun->getFont()->setSize(48);
$textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) );
// Create a shape (text)
echo date('H:i:s') . " Create a shape (rich text)\n";
$shape = $currentSlide->createRichTextShape();
$shape->setHeight(600);
$shape->setWidth(930);
$shape->setOffsetX(10);
$shape->setOffsetY(100);
$shape->getAlignment()->setHorizontal( PHPPowerPoint_Style_Alignment::HORIZONTAL_LEFT );
$textRun = $shape->createTextRun('Check the project site on CodePlex:');
$textRun->getFont()->setSize(36);
$textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) );
$shape->createBreak();
$textRun = $shape->createTextRun('  );
$textRun->getFont()->setSize(36);
$textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) );
// Create templated slide
echo date('H:i:s') . " Create templated slide\n";*/
//test
//从数据库调取数据进行for循环
$row=array('titlepic'=>array('./images/love.gif','./images/love1.gif','./images/love2.gif','./images/love3.gif'),'xsprice'=>array("55","33","22","333"),'cjid'=>array('100','222','333','3333'),'lpid'=>array('111','222','333','444'),'price'=>array('111','433','243','3245'));
for($i=0;$i<4;$i++)
{
$currentSlide = createTemplatedSlide1($objPHPPowerPoint,$row["titlepic"][$i]); // local function
// Create a shape (text)
//echo date('H:i:s') . " Create a shape (rich text)\n";
$shape = $currentSlide->createRichTextShape();
$shape->setHeight(100);
$shape->setWidth(930);
//调整字体的高度宽度
$shape->setOffsetX(20);
$shape->setOffsetY(400);
//$shape->getAlignment()->setHorizontal( PHPPowerPoint_Style_Alignment::HORIZONTAL_LEFT );
$row["price"]=iconv("utf-8","gb2312",$row["price"][$i]);
$textRun = $shape->createTextRun('礼品网价格:'.$row["xsprice"][$i]);
$textRun->getFont()->setBold(true);
$textRun->getFont()->setSize(48);
$textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( '#000000' ) );
$shape = $currentSlide->createRichTextShape();
$shape->setHeight(600);
$shape->setWidth(930);
$shape->setOffsetX(20);
$shape->setOffsetY(500);
$shape->getAlignment()->setHorizontal( PHPPowerPoint_Style_Alignment::HORIZONTAL_LEFT );
//添加多行内容从这开始
$textRun = $shape->createTextRun('公司编号:  '.$row["cjid"][$i]);
$textRun->getFont()->setSize(36);
$textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( '#000000' ) );
$shape->createBreak();
$textRun = $shape->createTextRun('礼品网编号:  '.$row["lpid"][$i]);
$textRun->getFont()->setSize(36);
$textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( '#000000' ) );
//test
// Save PowerPoint 2007 file
}
//echo date('H:i:s') . " Write to PowerPoint2007 format\n";
$objWriter = PHPPowerPoint_IOFactory::createWriter($objPHPPowerPoint, 'PowerPoint2007');
$objWriter->save(str_replace('.php', '.pptx',__FILE__));
header("Content-type:application/vnd.ms-powerpoint;");
header("location:02presentation.pptx");
// Echo memory peak usage
//echo date('H:i:s') . " Peak memory usage: " . (memory_get_peak_usage(true) / 1024 / 1024) . " MB\r\n";
// Echo done
//echo date('H:i:s') . " Done writing file.\r\n";
/**
* Creates a templated slide
*
* @param PHPPowerPoint $objPHPPowerPoint
* @return PHPPowerPoint_Slide
*/
function createTemplatedSlide1(PHPPowerPoint $objPHPPowerPoint,$cs1)
{
// Create slide
$slide = $objPHPPowerPoint->createSlide();
// Add background image
$shape = $slide->createDrawingShape();
$shape->setName('Background');
$shape->setDescription('Background');
$shape->setPath('./images/realdolmen_bg.jpg');
$shape->setWidth(950);
$shape->setHeight(720);
$shape->setOffsetX(0);
$shape->setOffsetY(0);
// Add logo
$shape = $slide->createDrawingShape();
$shape->setName('PHPPowerPoint logo');
$shape->setDescription('PHPPowerPoint logo');
$shape->setPath($cs1);
$shape->setHeight(120);
$shape->setOffsetX(10);
$shape->setOffsetY(10);
// Return slide
return $slide;
}

ログイン後にコピー

私の問題はまだ研究中です。より良い解決策がある場合は、投稿を続けて、より良い共有を楽しみにしています。

この記事が、PHP プログラミング設計に携わる皆様のお役に立てれば幸いです。

上記では、PHP で PPT ファイルを読み取る方法と、関連する内容を紹介しました。PHP チュートリアルに興味のある友人に役立つことを願っています。

関連ラベル:
gt
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!