我想把一个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');
}发布于 2020-09-09 01:51:53
我不认为这是你的代码的问题-更多的是底层库的问题-就像这里提到的:PhpPresentation imagecreatefromstring(): Data is not in a recognized format - PHP7.2
它运行了一个测试,看看它是不是我可以复制的东西-我能够。我测试中的主要区别是在一个演示文稿中我有一个简单的背景,而在另一个演示文稿中我有一个渐变。
此幻灯片引发了问题:

但这一条被复制得很好:

对于更复杂的背景,我得到了如下错误:
我的代码甚至比你的代码更简单,我只需克隆原始幻灯片,并在保存之前删除除一张幻灯片之外的所有幻灯片:
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');
}如果这不能完全解决您的问题,很抱歉,但希望它可以帮助确定为什么会发生这种情况。我链接到的另一个答案提供了有关在幻灯片中查找不支持的图像类型的更多信息。
发布于 2022-02-21 11:13:34
您可以尝试使用Aspose.Slides Cloud SDK for PHP将演示文稿拆分为单独的幻灯片,并将其保存为多种格式。您可以评估这个基于REST的API,每月进行150次免费API调用,用于API学习和演示处理。下面的代码示例显示如何使用Aspose.Slides云拆分演示文稿并将幻灯片保存为PPTX格式:
use Aspose\Slides\Cloud\Sdk\Api\Configuration;
use Aspose\Slides\Cloud\Sdk\Api\SlidesApi;
use Aspose\Slides\Cloud\Sdk\Model;
$configuration = new Configuration();
$configuration->setAppSid("my_client_id");
$configuration->setAppKey("my_client_key");
$slidesApi = new SlidesApi(null, $configuration);
$filePath = "example.pptx";
// Upload the file to the default storage.
$fileStream = fopen($filePath, 'r');
$slidesApi->uploadFile($filePath, $fileStream);
// Split the file and save the slides in PPTX format in the same folder.
$response = $slidesApi->split($filePath, null, Model\SlideExportFormat::PPTX);
// Download files of the slides.
foreach($response->getSlides() as $slide) {
$slideFilePath = pathinfo($slide->getHref())["basename"];
$slideFile = $slidesApi->downloadFile($slideFilePath);
echo $slideFile->getRealPath(), "\r\n";
}有时有必要在不使用任何代码的情况下拆分演示文稿。在这种情况下,您可以使用Online PowerPoint Splitter。
我在Aspose担任支持开发人员。
https://stackoverflow.com/questions/63677747
复制相似问题