stejaysulli / php-word-cat
用于操作 docx 文件的库
v0.1.11
2021-10-22 14:12 UTC
This package is auto-updated.
Last update: 2024-09-16 02:40:32 UTC
README
对 docx 文档的处理有限
一个简单的 PHP 库,用于操作 docx 文档;特别地,该库设计为允许将一个文档的内容插入到另一个文档中,它还提供了一些允许搜索和替换内容、插入新文本和图像的功能。
依赖关系
此库需要 DOMDocument
和 SimpleXML
。
功能
此库是为了解决我在创建新文档时使用(不提名字)另一个流行的 PHP 库时遇到的一些特定问题而设计的;因此,有很多功能根本就没有实现,因为我没有需要它们。我已在下面添加了一个“限制”部分来列出一些特别明显的功能,您可能想要但缺少的功能,但如果您想要的功能在下面的项目符号列表中找不到,请认为库无法为您完成这项功能!
- 打开 docx 文档
- 从文档中提取和读取 XML 文件
- 将 XML 文件写入文档
- 操作文档文件中的 XML
- 保存 docx,可以覆盖原始文件或作为新文档
- 搜索文本,可以是二进制/纯文本搜索或正则表达式
- 替换文本,可以是二进制/纯文本搜索或正则表达式
- 插入包含纯文本的段落
- 插入指定大小(保持长宽比,同时将图像约束在指定大小内)的图像文件
- 将一个 docx 的内容插入到另一个中
限制
由于此库旨在解决一些相当具体的问题,因此它不会做很多您可能需要的事情;我可能会也可能不会添加功能
- 您不能从头创建新文档;您需要加载现有的 docx 文件来工作
- 该库很天真;大多数操作都需要了解 docx 文件的内部 XML 结构
- 提供的抽象是为执行特定任务而设计的(参见 tests/example.php 以了解这一点);为了更通用,它们可能需要进行重大更改,所以如果您现在正在使用此库,请确保锁定到特定版本!
安装
您可以使用 composer 安装此库
composer require stejaysulli/php-word-cat
应该可以在没有 composer 的情况下使用此库,但这尚未经过测试,您需要提供自己的方法来自动加载 src
目录中的文件。
使用
所有基本功能的一个很好的示例可以在 tests/example.php 中找到 - 建议检查该示例以获取正确的使用细节。为了让您了解使用 WordCat 编写的代码类型,这里有一个简短的示例
<?php use WordCat\WordCat; $wordcat = WordCat::instance("example1.doc"); // Find XML elements containing the text "apple", "orange" or "pear" $wordcat->findText("apple") ->andFindText("orange") ->andFindText("pear") ->forSearch(function($element) { // Print the text content of each element: echo "{$element->textContent}\n"; }); // Get an array of all the matching XML elements (DOMNode objects): $results = $wordcat->getSearch(); // Clear the search results: $wordcat->clearSearch(); // Do the same, but use a regex this time, and make it case insensitive: $wordcat->findRegex("/(apple|orange|pear)/i") ->forSearch(function($element) { echo "{$element->textContent}\n"; }); // Replace all instances of the word "wordcat" with "WordCat": $wordcat->clearSearch()->replaceText("wordcat", "WordCat"); // Use a regex to replace anything that's formatted like a Y-m-d date with // today's date: $wordcat->clearSearch()->replaceRegex("/[0-9]{4}-[0-9]{2}-[0-9]{2}/", date("Y-m-d")); // Insert some paragraphs after each paragraph containing some text: $wordcat->findText("inserted paragraph next") ->forSearch( function($insertionPoint) use($wordcat, $imageFile) { // Insert a simple paragraph containing one text run: $p1 = $wordcat->insertParagraph( "This is the first inserted paragraph", $insertionPoint ); } ); // Get an element to insert the document after: $search = $wordcat->findText("INSERT DOCUMENT HERE")->getSearch(); // Open a document to insert $wordcatSource = WordCat::instance("example2.docx"); // Check we have at least one insertion point: if(count($search) > 0) { // Use the first insertion point: $wordcat->insertDocument($wordcatSource, $search[0]); // Remove the insertion point element to get rid of the "INSERT DOCUMENT // HERE" text: $wordcat->removeNode($search[0]); } // Save a new docx file $wordcat->saveAs("test-output.docx"); // Close the WordCat instance $wordcat->close(); $wordcatSource->close();