label305/pptx-extractor

PHP 库,用于提取和替换 .pptx 文件中的字符串数据。

0.2.4 2024-04-30 12:27 UTC

This package is auto-updated.

Last update: 2024-08-30 13:18:27 UTC


README

PHP 库,用于提取和替换 .pptx 文件中的字符串数据。.pptx 文件是填充有 XML 文档和资源的 zip 存档。它们的格式由 OOXML 描述。此库仅操作 ppt/slide.xml(例如 slide1.xml, slide2.xml)文件。

Composer 安装

"require": {
    "label305/pptx-extractor": "0.2.*"
}

要求

  • PHP 8.0
  • PHP ext-dom
  • PHP ext-zip
  • PHP ext-libxml

基本用法

导入基本类。

use Label305\PptxExtractor\Basic\BasicExtractor;
use Label305\PptxExtractor\Basic\BasicInjector;

首先,我们需要从现有的 pptx 文件中提取所有内容。这可以通过使用 BasicExtractor 完成。调用 extractStringsAndCreateMappingFile 将创建一个新文件,其名称作为第二个参数传递。这个新文件包含引用,这样库就知道稍后如何将更改后的文本重新注入。

$extractor = new BasicExtractor();
$mapping = $extractor->extractStringsAndCreateMappingFile(
    'simple-slides.pptx',
    'simple-slides-extracted.pptx'
  );

现在您已经提取了内容,您可以检查结果 $mapping 数组的内容。如果您想更改内容,可以简单地修改它。数组键映射到 simple-slides-extracted.pptx 中的一个符号。

echo $mapping[0][0]; // Slide number one

现在,更改了内容后,您可以将其保存回新的文件。在这种情况下,该文件是 simple-slides-injected.pptx

$mapping[0][0] = "Slide number one";

$injector = new BasicInjector();
$injector->injectMappingAndCreateNewFile(
    $mapping,
    'simple-slides-extracted.pptx',
    'simple-slides-injected.pptx'
  );

高级用法

此库还配备了 DecoratedTextExtractorDecoratedTextInjector,可以用于操作基本的段落样式,如加粗、斜体和下划线。您还可以使用 SharedString 对象来区分文本的逻辑分组。

$extractor = new DecoratedTextExtractor();
$mapping = $extractor->extractStringsAndCreateMappingFile(
    'markup.pptx',
    'markup-extracted.pptx'
  );
  
$firstParagraph = $mapping[0]; // Paragraph object
$$firstTextRun = $firstParagraph[0]; // TextRun object

$firstTextRun->italic = true;
$firstTextRun->bold = false;
$firstTextRun->underline = true;

echo $firstTextRun->text; // The quick brown fox jumps over the lazy dog
$firstTextRun->text = "Several fabulous dixieland jazz groups played with quick tempo.";

$injector = new DecoratedTextInjector();
$injector->injectMappingAndCreateNewFile(
    $mapping,
    'markup-extracted.pptx',
    'markup-injected.pptx'
  );

许可证

版权所有 2020 Label305 B.V.

根据 Apache License,版本 2.0(“许可证”);除非遵守许可证规定,否则不得使用此文件。您可以在以下位置获得许可证副本:

https://apache.ac.cn/licenses/LICENSE-2.0

除非适用法律要求或书面同意,否则在许可证下分发的软件按“原样”基础分发,不提供任何明示或暗示的保证或条件。有关许可证的具体语言、权限和限制,请参阅许可证。