问题描述
我想将一个pptx文件的幻灯片拆分为单独的pptx文件,每个文件包含一张幻灯片。内容/文本已复制,但布局和样式未复制。这是代码。
任何人都可以帮忙吗?
<?php
use PhpOffice\PhpPresentation\PhpPresentation;
use PhpOffice\PhpPresentation\IOFactory;
use PhpOffice\PhpPresentation\Style\Color;
use PhpOffice\PhpPresentation\Style\Alignment;
use PhpOffice\PhpPresentation\Slide\SlideLayout;
$objReader = \PhpOffice\PhpPresentation\IOFactory::createReader('PowerPoint2007');
$objPHPPowerPoint = $objReader->load('a.pptx');
$totalSlides = $objPHPPowerPoint->getSlideCount();
$oMasterSlide = $objPHPPowerPoint->getAllMasterSlides()[0];
$documentProperties = $objPHPPowerPoint->getDocumentProperties();
for ( $count = 0; $count < $totalSlides; $count++ ) {
$objPHPPresentation = new PhpPresentation();
$slide = $objPHPPowerPoint->getSlide( $count );
$background = $slide->getBackground();
$newSlide = $objPHPPresentation->addSlide( $slide );
$newSlide->setBackground ( $background );
$objPHPPresentation->setAllMasterSlides( $oMasterSlide );
$objPHPPresentation->removeSlideByIndex(0);
$oWriterPPTX = \PhpOffice\PhpPresentation\IOFactory::createWriter($objPHPPresentation,'PowerPoint2007');
$oWriterPPTX->save($count.'.pptx');
}
解决方法
我认为这与您的代码无关,而与基础库无关,如此处所述:PhpPresentation imagecreatefromstring(): Data is not in a recognized format - PHP7.2
它进行了测试,看是否可以复制—并且能够复制。测试的主要区别在于,一个演示文稿的背景很简单,而另一个演示文稿的背景是渐变的。
在更复杂的背景下,我出现了以下错误:
PHP警告:imagecreatefromstring():数据不是可识别的格式
我的代码比您的代码还要简单,我只是克隆了原始幻灯片并在保存之前删除了除单个幻灯片之外的所有幻灯片:
for ( $count = 0; $count < $totalSlides; $count++ ) {
$copyVersion = clone $objPHPPowerPoint;
foreach ($copyVersion->getAllSlides() as $index => $slide) {
if ($index !== $count) {
$copyVersion->removeSlideByIndex($index);
}
}
$oWriterPPTX = \PhpOffice\PhpPresentation\IOFactory::createWriter($copyVersion,'PowerPoint2007');
$oWriterPPTX->save($count.'.pptx');
}
很抱歉,这不能完全解决您的问题,但希望它可以帮助您确定问题的原因。我链接到的另一个答案提供了有关在幻灯片中查找不受支持的图像类型的更多信息。