PHP读取PPT文件的方法


Posted in PHP onDecember 10, 2015

本文实例讲述了PHP读取PPT文件的方法。分享给大家供大家参考,具体如下:

最近做一个和FLASH有关的东西,其中就要用到在网站上看PPT就像百度,豆丁网那样可以直接在网站上读,在网上搜了半天没搜到,都是些什么安装个软件什么的,PHP网站放到空间上,谁能让你在哪装软件呢?不是在瞎扯么?不过还好,最后在国外一个网站上搜到了一个解决思路,就是一个PHP操作PPT的类,当然这个网站还提供了操作OFFICES软件的其他类,不过是2007版的OFFICES,现把网址贴出来奉献给大家:http://phppowerpoint.codeplex.com/。也可点击此处本站下载。

这个类可以实现 PPT2007格式的读写/生成

下面也贴出当时看到的一个例子:

本例子使用phppowerpoint类,使用该类注意两点,一个是了解参数含义,二注意字符集

<?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 相关文章推荐
实现dedecms全站URL静态化改造的代码
Mar 29 PHP
c#中的实现php中的preg_replace
Dec 21 PHP
php中通过正则表达式下载内容中的远程图片的函数代码
Jan 10 PHP
查找mysql字段中固定字符串并替换的几个方法
Sep 23 PHP
编写安全 PHP应用程序的七个习惯深入分析
Jun 08 PHP
php addslashes 利用递归实现使用反斜线引用字符串
Aug 05 PHP
PHP 快速排序算法详解
Nov 10 PHP
PHP 微信支付类 demo
Nov 30 PHP
PHP数组游标实现对数组的各种操作详解
Jan 26 PHP
PHP实现的Redis多库选择功能单例类
Jul 27 PHP
PHP实现正则表达式分组捕获操作示例
Feb 03 PHP
PHP实现微信公众号验证Token的示例代码
Dec 16 PHP
关于WordPress的SEO优化相关的一些PHP页面脚本技巧
Dec 10 #PHP
PHP创建PowerPoint2007文档的方法
Dec 10 #PHP
判断、添加和删除WordPress置顶文章的相关PHP函数小结
Dec 10 #PHP
调试WordPress中定时任务的相关PHP脚本示例
Dec 10 #PHP
在WordPress中使用PHP脚本来判断访客来自什么国家
Dec 10 #PHP
修改PHP脚本使WordPress拦截垃圾评论的方法示例
Dec 10 #PHP
php获取图片信息的方法详解
Dec 10 #PHP
You might like
Apache, PHP在Windows 9x/NT下的安装与配置 (一)
2006/10/09 PHP
php 验证码(倾斜,正弦干扰线,黏贴,旋转)
2013/06/29 PHP
linux下使用crontab实现定时PHP计划任务失败的原因分析
2014/07/05 PHP
Symfony2在Nginx下的配置方法图文教程
2016/02/04 PHP
HTML页面如何象ASP一样接受参数
2007/02/07 Javascript
jQueryUI的Dialog的简单封装
2010/06/07 Javascript
jQuery EasyUI API 中文文档 - Calendar日历使用
2011/10/19 Javascript
JS通过相同的name进行表格求和代码
2013/08/18 Javascript
jQuery 计算iframe 窗口大小的方法
2014/05/13 Javascript
JavaScript eval() 函数介绍及应用示例
2014/07/29 Javascript
修改或扩展jQuery原生方法的代码实例
2015/01/13 Javascript
关于网页中的无缝滚动的js代码
2016/06/09 Javascript
jquery设置表单元素为不可用的简单代码
2016/07/04 Javascript
jQuery实现字符串全部替换的方法
2016/12/12 Javascript
AngularJS的ng-repeat指令与scope继承关系实例详解
2017/01/21 Javascript
js从输入框读取内容,比较两个数字的大小方法
2017/03/13 Javascript
基于node.js制作简单爬虫教程
2017/06/29 Javascript
详解node服务器中打开html文件的两种方法
2017/09/18 Javascript
vue-自定义组件传值的实例讲解
2018/09/18 Javascript
Vue组件通信中非父子组件传值知识点总结
2019/12/05 Javascript
[00:37]食人魔魔法师轮盘吉兆顺应全新至宝将拥有额外款式
2019/12/19 DOTA
Python利用matplotlib生成图片背景及图例透明的效果
2017/04/27 Python
Python中音频处理库pydub的使用教程
2017/06/07 Python
python脚本作为Windows服务启动代码详解
2018/02/11 Python
对python 生成拼接xml报文的示例详解
2018/12/28 Python
python flask中动态URL规则详解
2019/11/22 Python
Python celery原理及运行流程解析
2020/06/13 Python
浅谈Python协程
2020/06/17 Python
Python基于gevent实现文件字符串查找器
2020/08/11 Python
Python机器学习工具scikit-learn的使用笔记
2021/01/28 Python
印度购物网站:TATA CLiQ
2017/11/23 全球购物
俄罗斯宠物用品网上商店:ZooMag
2019/12/12 全球购物
5.12护士节演讲稿
2014/04/30 职场文书
美丽家庭事迹材料
2014/05/03 职场文书
教师工作决心书
2015/02/04 职场文书
2015年学校政教工作总结
2015/07/20 职场文书