PHPWord

用于读写文字处理文档的纯PHP库。(A pure PHP library for reading and writing word processing documents.)

Github stars Tracking Chart

PHPWord是一个用纯PHP编写的库,它提供了一组类以便从不同的文档文件格式中读写。当前版本的PHPWord支持Microsoft Office Open XML (OOXML或OpenXML),OASIS 打开Office应用程序的文档格式(OpenDocument或ODF), Rich Text Format (RTF),HTML和PDF。

PHPWord是一个根据 LGPL第3版条款许可的开源项目。 PHPWord旨在通过整合持续集成单元测试来成为高质量的软件产品。您可以阅读开发人员文档了解更多关于PHPWord的信息。

如果您有任何问题,请在 StackOverFlow 上询问。

功能

使用PHPWord,您可以使用PHP 5.3.3+脚本动态创建OOXML、ODF或RTF文档。下面是你可以用PHPWord库做的一些事情:

  • 设定文件属性,例如标题,主题和创建者。
  • 建立不同设定的文件区段,例如肖像/风景,页面大小和页面编号
  • 为每个部分创建页眉和页脚
  • 设置默认字体类型,字体大小和段落样式
  • 使用UTF-8和东亚字体/字符
  • 定义自定义字体样式(例如粗体,斜体,颜色)和段落样式(例如居中,多列,间距)
  • 插入段落,可以是包含其他元素的简单文本或复杂文本(文本运行)
  • 插入标题(标题)和目录
  • 插入文字分页和分页符
  • 插入并格式化图片,包括本地图片,远程图片或页面水印
  • 插入二进制OLE对象,例如Excel或Visio
  • 为每行(例如重复为标题行)和单元格(例如背景颜色,rowspan,colspan)插入并格式化具有自定义属性的表
  • 将列表项目插入为项目符号,编号或多级
  • 插入超链接
  • 插入脚注和尾注
  • 插入绘图形状(圆弧,曲线,直线,折线,矩形,椭圆形)
  • 插入图表(饼图,甜甜圈,酒吧,线,区域,分散,雷达)
  • 插入表单字段(textinput,复选框和下拉列表)
  • 从模板创建文档
  • 使用XSL 1.0样式表转换OOXML模板的标题,主文档部分和页脚
  • ...以及更多进展功能

要求

PHPWord需要以下内容:

安装

PHPWord通过Composer安装。 要在您的项目中 为PHPWord添加依赖项

运行以下命令以使用最新的稳定版本

composer require phpoffice/phpword

或者如果你想要最新的主版本

composer require phpoffice/phpword:dev-master

您当然也可以手动编辑您的composer.json文件

{
    "require":{
       “phpoffice/phpword”:“v0.14.*”
    }
}

开始

以下是PHPWord库的基本用法示例。

<?php
require_once 'bootstrap.php';
//创建新文档...
$phpWord = new \PhpOffice\PhpWord\PhpWord();
/* 注意:您附加到文档的任何元素都必须位于节中。 */
// 在文档中添加一个空白部分...
$section = $phpWord->addSection();
// 将文本元素添加到缺省字体样式的部分...
$section->addText(
    '"Learn from yesterday, live for today, hope for tomorrow. '
        . 'The important thing is not to stop questioning." '
        . '(Albert Einstein)'
);
/*
 * 注意:可以通过三种方式自定义添加的Text元素的字体样式:
 * - inline;
 * - 使用指定的字体样式(隐式创建新的字体样式对象);
 * - 使用明确创建的字体样式对象。
 */
// 添加带有自定义内联字体的文本元素...
$section->addText(
    '"Great achievement is usually born of great sacrifice, '
        . 'and is never the result of selfishness." '
        . '(Napoleon Hill)',
    array('name' => 'Tahoma', 'size' => 10)
);
// 添加使用指定字体样式自定义字体的文本元素...
$fontStyleName = 'oneUserDefinedStyle';
$phpWord->addFontStyle(
    $fontStyleName,
    array('name' => 'Tahoma', 'size' => 10, 'color' => '1B2232', 'bold' => true)
);
$section->addText(
    '"The greatest accomplishment is not in never falling, '
        . 'but in rising again after you fall." '
        . '(Vince Lombardi)',
    $fontStyleName
);
// 使用明确创建的字体样式对象自定义字体添加文本元素...
$fontStyle = new \PhpOffice\PhpWord\Style\Font();
$fontStyle->setBold(true);
$fontStyle->setName('Tahoma');
$fontStyle->setSize(13);
$myTextElement = $section->addText('"Believe you can and you\'re halfway there." (Theodor Roosevelt)');
$myTextElement->setFontStyle($fontStyle);
// 将文档保存为OOXML文件...
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('helloWorld.docx');
// 将文档保存为ODF文件...
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'ODText');
$objWriter->save('helloWorld.odt');
// 将文档保存为HTML文件...
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'HTML');
$objWriter->save('helloWorld.html');
/* 注意:我们跳过RTF,因为它不是基于XML的,需要一个不同的例子。 */
/* 注意:我们跳过PDF,因为“HTML-to-PDF”方法用于创建PDF文档。*/

示例文件夹中提供了更多示例。要轻松访问这些示例,请在示例目录中启动 php -S localhost:8000 ,然后浏览至http://localhost:8000查看样本。 您也可以阅读 开发人员文档 API文档了解更多详情。

贡献

我们欢迎大家为PHPWord做出贡献。以下是您可以做出贡献的一些内容。

Overview

Name With OwnerPHPOffice/PHPWord
Primary LanguagePHP
Program languagePHP (Language Count: 4)
PlatformLinux, Mac, Windows, BSD
License:Other
Release Count24
Last Release Name1.2.0 (Posted on )
First Release Name0.7.0 (Posted on )
Created At2012-05-06 16:04:30
Pushed At2024-04-12 12:18:02
Last Commit At2024-01-23 09:15:27
Stargazers Count7.1k
Watchers Count312
Fork Count2.7k
Commits Count2.3k
Has Issues Enabled
Issues Count1731
Issue Open Count1068
Pull Requests Count441
Pull Requests Open Count107
Pull Requests Close Count324
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private

PHPWord

Master:
Latest Stable Version
Build Status
Code Quality
Coverage Status
Total Downloads
License
Join the chat at https://gitter.im/PHPOffice/PHPWord

Develop:
Latest Development Version
Build Status
Code Quality
Coverage Status

PHPWord is a library written in pure PHP that provides a set of classes to write to and read from different document file formats. The current version of PHPWord supports Microsoft Office Open XML (OOXML or OpenXML), OASIS Open Document Format for Office Applications (OpenDocument or ODF), Rich Text Format (RTF), HTML, and PDF.

PHPWord is an open source project licensed under the terms of LGPL version 3. PHPWord is aimed to be a high quality software product by incorporating continuous integration and unit testing. You can learn more about PHPWord by reading the Developers' Documentation.

If you have any questions, please ask on StackOverFlow

Read more about PHPWord:

Features

With PHPWord, you can create OOXML, ODF, or RTF documents dynamically using your PHP 5.3.3+ scripts. Below are some of the things that you can do with PHPWord library:

  • Set document properties, e.g. title, subject, and creator.
  • Create document sections with different settings, e.g. portrait/landscape, page size, and page numbering
  • Create header and footer for each sections
  • Set default font type, font size, and paragraph style
  • Use UTF-8 and East Asia fonts/characters
  • Define custom font styles (e.g. bold, italic, color) and paragraph styles (e.g. centered, multicolumns, spacing) either as named style or inline in text
  • Insert paragraphs, either as a simple text or complex one (a text run) that contains other elements
  • Insert titles (headers) and table of contents
  • Insert text breaks and page breaks
  • Insert and format images, either local, remote, or as page watermarks
  • Insert binary OLE Objects such as Excel or Visio
  • Insert and format table with customized properties for each rows (e.g. repeat as header row) and cells (e.g. background color, rowspan, colspan)
  • Insert list items as bulleted, numbered, or multilevel
  • Insert hyperlinks
  • Insert footnotes and endnotes
  • Insert drawing shapes (arc, curve, line, polyline, rect, oval)
  • Insert charts (pie, doughnut, bar, line, area, scatter, radar)
  • Insert form fields (textinput, checkbox, and dropdown)
  • Create document from templates
  • Use XSL 1.0 style sheets to transform headers, main document part, and footers of an OOXML template
  • ... and many more features on progress

Requirements

PHPWord requires the following:

Installation

PHPWord is installed via Composer.
To add a dependency to PHPWord in your project, either

Run the following to use the latest stable version

    composer require phpoffice/phpword

or if you want the latest master version

    composer require phpoffice/phpword:dev-master

You can of course also manually edit your composer.json file

{
    "require": {
       "phpoffice/phpword": "v0.16.*"
    }
}

Getting started

The following is a basic usage example of the PHPWord library.

<?php
require_once 'bootstrap.php';

// Creating the new document...
$phpWord = new \PhpOffice\PhpWord\PhpWord();

/* Note: any element you append to a document must reside inside of a Section. */

// Adding an empty Section to the document...
$section = $phpWord->addSection();
// Adding Text element to the Section having font styled by default...
$section->addText(
    '"Learn from yesterday, live for today, hope for tomorrow. '
        . 'The important thing is not to stop questioning." '
        . '(Albert Einstein)'
);

/*
 * Note: it's possible to customize font style of the Text element you add in three ways:
 * - inline;
 * - using named font style (new font style object will be implicitly created);
 * - using explicitly created font style object.
 */

// Adding Text element with font customized inline...
$section->addText(
    '"Great achievement is usually born of great sacrifice, '
        . 'and is never the result of selfishness." '
        . '(Napoleon Hill)',
    array('name' => 'Tahoma', 'size' => 10)
);

// Adding Text element with font customized using named font style...
$fontStyleName = 'oneUserDefinedStyle';
$phpWord->addFontStyle(
    $fontStyleName,
    array('name' => 'Tahoma', 'size' => 10, 'color' => '1B2232', 'bold' => true)
);
$section->addText(
    '"The greatest accomplishment is not in never falling, '
        . 'but in rising again after you fall." '
        . '(Vince Lombardi)',
    $fontStyleName
);

// Adding Text element with font customized using explicitly created font style object...
$fontStyle = new \PhpOffice\PhpWord\Style\Font();
$fontStyle->setBold(true);
$fontStyle->setName('Tahoma');
$fontStyle->setSize(13);
$myTextElement = $section->addText('"Believe you can and you\'re halfway there." (Theodor Roosevelt)');
$myTextElement->setFontStyle($fontStyle);

// Saving the document as OOXML file...
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('helloWorld.docx');

// Saving the document as ODF file...
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'ODText');
$objWriter->save('helloWorld.odt');

// Saving the document as HTML file...
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'HTML');
$objWriter->save('helloWorld.html');

/* Note: we skip RTF, because it's not XML-based and requires a different example. */
/* Note: we skip PDF, because "HTML-to-PDF" approach is used to create PDF documents. */

More examples are provided in the samples folder. For an easy access to those samples launch php -S localhost:8000 in the samples directory then browse to http://localhost:8000 to view the samples.
You can also read the Developers' Documentation for more detail.

Contributing

We welcome everyone to contribute to PHPWord. Below are some of the things that you can do to contribute.

To the top