1. 产品
  2.   字处理
  3.   PHP
  4.   MDword
 
  

免费 PHP 库,用于创建和管理基于模板的 DOCX 文件

领先的开源 PHP 库,支持从模板创建、读取、修改和操作 Microsoft Word(DOCX)文档。通过免费 PHP API 添加表格、插入文本、图像并应用格式

Go-Docx 库是什么?

在 Golang 开发的世界里,处理 Microsoft Word 文档(.docx)历来是一个挑战。许多现有库要么只能“写入”,要么价格昂贵。Go-Docx(由 fumiama 维护)弥补了这一空白,是最具功能性的开源库之一,旨在读取和写入 ECMA-376 Office Open XML 文件。它有用,因为它提供了一个高级 API,抽象了 Word 文件底层的复杂 XML 结构。

使 Go-Docx 脱颖而出的是其全面的功能集:文本格式化(颜色、大小、对齐)、图片插入、表格操作、形状、画布和分组。无论是生成发票、解析简历,还是自动化报告生成,该库都能在不调用外部应用的情况下处理复杂的 Office Open XML(ECMA-376)结构。它是一个由社区驱动的分支,已显著进化于其前身。不同于需要付费才能获取完整功能的商业替代品如 UniOffice,Go-Docx 在 AGPL‑3.0 许可证下完全免费。

Previous Next

Go-Docx 入门指南

将 Go-Docx 引入项目的推荐方式是使用 GitHub。请使用以下命令进行顺畅的安装。

通过 GitHub 安装 Go-Docx

composer require mkdreams/mdword  

如何使用 Go 库生成 Word Docx 文件?

git clone https://github.com/mkdreams/MDword.git 

使用 Go 进行高级文档生成

Go-Docx 允许您使用结构化方法从零创建复杂文档。它负责初始化文档环境,包括默认主题和样式,确保生成的文件完全兼容 Microsoft Word 及其他现代处理器。以下是一个简单示例,演示如何在 Go 应用中生成 Word Docx 文件。

如何使用 Go 库解析 Word Docx 文档?

require_once 'vendor/autoload.php';

use MDword\WordProcessor;

// Load a template
$template = new WordProcessor();
$template->load('template.docx');

// Replace placeholders
$template->setValue('{{NAME}}', 'John Doe');
$template->setValue('{{DATE}}', date('Y-m-d'));

// Save the modified document
$template->save('output.docx');

通过 Go API 进行全面的文档解析

与许多竞争对手不同,开源的 Go-Docx 库在读取现有文件方面表现出色。它可以解析 .docx 文件并遍历其正文项,如段落和表格。这使其成为数据提取或文档审计任务的理想选择,需要以编程方式分析内容。以下示例展示了软件开发者如何在自己的 Go 应用中解析 Word 文档。

如何使用 Go 库在 Word 文件中创建简单表格?

require 'vendor/autoload.php';
use Mkdreams\MDword\MDword;
use Mkdreams\MDword\Elements\TextRun;
use Mkdreams\MDword\Elements\Paragraph;

$mdword = new MDword();
$section = $mdword->createSection(); // Sections are often containers for content

// Add a simple paragraph
$paragraph1 = new Paragraph();
$paragraph1->addText('This is a simple paragraph.');
$section->addElement($paragraph1);

// Add a paragraph with formatted text
$paragraph2 = new Paragraph();
$textRun1 = new TextRun();
$textRun1->setText('This text is ');
$paragraph2->addElement($textRun1);

$textRun2 = new TextRun();
$textRun2->setText('bold and red.');
$textRun2->setFontStyle(['bold' => true, 'color' => 'FF0000']); // Color as hex
$paragraph2->addElement($textRun2);
$section->addElement($paragraph2);

// Add text with specific font and size
$paragraph3 = new Paragraph();
$textRun3 = new TextRun();
$textRun3->setText('This is Arial, size 16.');
$textRun3->setFontStyle(['name' => 'Arial', 'size' => 16]);
$paragraph3->addElement($textRun3);
$section->addElement($paragraph3);

$mdword->save('FormattedTextDocument.docx');
echo "Formatted document created.";

通过 Go 在 Docx 文件中动态构建表格

对排版的细粒度控制是此开源 Go 库的核心优势。您可以通过 Go-Docx 修改文本颜色、字体大小、对齐方式,甚至添加超链接或制表位。这使开发者能够生成专业且符合特定设计要求的品牌文档,而无需手动干预。

如何使用 Go 库对 Word 文档应用文本样式和格式化?

require 'vendor/autoload.php';
use Mkdreams\MDword\MDword;
use Mkdreams\MDword\Elements\Table;
use Mkdreams\MDword\Elements\Row;
use Mkdreams\MDword\Elements\Cell;
use Mkdreams\MDword\Elements\TextRun;

$mdword = new MDword();
$section = $mdword->createSection();

// Create a table
$table = new Table();
// Potentially add table-level styling, e.g., borders
// $table->setStyle(['borderSize' => 6, 'borderColor' => '000000']);

// Add a header row
$headerRow = new Row();
$cellH1 = new Cell();
$cellH1->addTextRun(new TextRun('Header 1', ['bold' => true]));
$headerRow->addCell($cellH1);

$cellH2 = new Cell();
$cellH2->addTextRun(new TextRun('Header 2', ['bold' => true]));
$headerRow->addCell($cellH2);
$table->addRow($headerRow);

// Add a data row
$dataRow1 = new Row();
$cellR1C1 = new Cell();
$cellR1C1->addTextRun(new TextRun('Data A1'));
$dataRow1->addCell($cellR1C1);

$cellR1C2 = new Cell();
$cellR1C2->addTextRun(new TextRun('Data B1'));
$dataRow1->addCell($cellR1C2);
$table->addRow($dataRow1);

$section->addElement($table);
$mdword->save('DocumentWithTable.docx');
echo "Document with table created.";

 中国人