PHPWord

PHPOffice活跃维护

纯 PHP 实现的 Word 文档读写库,无需 Office 环境即可生成和解析 docx、odt、rtf 等格式,适合在服务端批量导出报表或合同。

A pure PHP library for reading and writing word processing documents

1.4.0发布于 2025-06-05 10:55 UTC7,589star2,731fork同步于 2026-11-10 17:33 UTC

怎么上手

由 README 提炼,执行前请确认
composer require phpoffice/phpword
硬件要求按 README 说明

README 未说明硬件要求;运行环境为 PHP 7.1+,需 XML Parser 扩展,Zip、GD、XMLWriter、XSL 扩展按需启用

下载:源码 zip·GitHub Releases(安装包)本站不镜像安装包

项目说明

由 README 自动翻译提炼

纯 PHP 库,用代码读写 Word 文档,支持 OOXML、ODF、RTF、HTML 和 PDF

纯 PHP 实现的 Word 文档读写库,无需 Office 环境即可生成和解析 docx、odt、rtf 等格式,适合在服务端批量导出报表或合同。

核心能力

  • 用纯 PHP 类动态生成 OOXML、ODF、RTF 文档
  • 支持页眉页脚、分节、页面方向与页码设置
  • 插入表格、图片、图表、形状、超链接与脚注
  • 支持模板填充与 XSL 样式表转换 OOXML 模板
  • 可导出 HTML,并借助 dompdf 生成 PDF

容易踩的坑

写 OOXML 和 ODF 需额外启用 Zip 与 XMLWriter 扩展;加图片需 GD 扩展,导出 PDF 需另装 dompdf;PDF 走的是 HTML-to-PDF 路径,并非直接写 PDF

内容由模型根据 README 生成,可能不完整或不准确,以官方文档为准。看原文 README

最近发版

5 个版本1.4.0
  1. 1.4.0最新正式版2025-06-05 · 2025-06-05 10:55 UTC

    Full Changelog

    Enhancements

    • Default Font: Allow specify Asisn font and Latin font separately
    • Writer ODText: Support for ListItemRun by @Progi1984 fixing #2159, #2620 in #2669
    • Writer HTML: Support for vAlign in Tables by @SpraxDev in #2675
    • Writer Word2007: Support for padding in Table Cell by @Azamat8405 in #2697
    • Added support for PHP 8.4 by @Progi1984 in #2660
    • Autoload : Allow to use PHPWord without Composer fixing #2543, #2552, #2716, #2717 in #2722
    • Add Default font color for Word by @Collie-IT in #2700
    • Writer HTML: Support Default font color by @MichaelPFrey in #2731
    • Writer ODText: Support Default font color by @MichaelPFrey in #2735
    • Add basic ruby text (phonetic guide) support for Word2007 and HTML Reader/Writer, RTF Writer, basic support for ODT writing by @Deadpikle in #2727
    • Reader HTML: Support font styles for h1/h6 by @Progi1984 fixing #2619 in #2737
    • Writer EPub3: Basic support by @Sambit003 fixing #55 in #2724
    • Writer Word2007: Added support for background and border color transparency in Text Box element by @chudy20007 in #2555
    • Writer/Reader Word2007: Added support for the firstLineChars for Indentation by @liuzhilinux & @Progi1984 in #2753

    Bug fixes

    • Writer ODText: Support for images inside a textRun by @Progi1984 fixing #2240 in #2668
    • Allow vAlign and vMerge on Style\Cell to be set to null by @SpraxDev fixing #2673 in #2676
    • Reader HTML: Support for differents size units for table by @Progi1984 fixing #2384, #2701 in #2725
    • Reader Word2007: Respect paragraph indent units by @tugmaks & @Progi1984 fixing #507 in #2726
    • Reader Word2007: Support Header elements within Title elements by @SpraxDev fixing #2616, #2426 in #2674
    • Reader HTML: Support for inherit value for property line-height by @Progi1984 fixing #2683 in #2733
    • Writer HTML: Fixed null string for Text Elements by @armagedon007 and @Progi1984 in #2738
    • Template Processor: Fix 0 considered as empty string by @cavasinf, @SnipsMine and @Progi1984 fixing #2572, #2703 in #2748
    • Word2007 Writer : Corrected generating TOC to fix page number missing issues @jgiacomello in #2556
    • Enhanced error handling in encoding functions @michalschroeder in #2784

    Miscellaneous

    • Bump dompdf/dompdf from 2.0.4 to 3.0.0 by @dependabot fixing #2621 in #2666
    • Add test case to make sure vMerge defaults to 'continue' by @SpraxDev in #2677
    • Adding the possibility to use iterate search and replace with setValues by @moghwan in #2632
    • Add test cases that test the ODTText and Word2007 reader using the corresponding writer, increasing test coverage by @MichaelPFrey in #2745
    • Updated TOCTest to use static HTML content instead of external resource @michalschroeder in #2784
    • Bump PHPOffice/math from 0.2.0 to 0.3.0 by @eileenmcnaughton in #2785

    Deprecations

    • Deprecate PhpOffice\PhpWord\Style\Paragraph::getIndent() : Use PhpOffice\PhpWord\Style\Paragraph::getIndentLeft()
    • Deprecate PhpOffice\PhpWord\Style\Paragraph::setHanging() : Use PhpOffice\PhpWord\Style\Paragraph::setIndentHanging()
    • Deprecate PhpOffice\PhpWord\Style\Paragraph::setIndent() : Use PhpOffice\PhpWord\Style\Paragraph::setIndentLeft()

    BC Breaks

    Notes

    • Writer ODText previously used to set 'style:use-window-font-color' to 'true', now it is set to 'false'. (see #2735) The effect of this attribute is "implementation dependent" (if implemented at all). Setting it to false allows setting a default font color and improves interoperabilt, but may break certain specific use cases.
  2. 1.3.02024-09-01 · 2024-09-01 07:38 UTC
    查看更新说明

    Full Changelog: https://github.com/PHPOffice/PHPWord/compare/1.2.0...1.3.0

    Enhancements

    • IOFactory : Added extractVariables method to extract variables from a document @sibalonat in #2515
    • PDF Writer : Documented how to specify a PDF renderer, when working with the PDF writer, as well as the three available choices by @settermjd in #2642
    • Word2007 Reader: Support for Paragraph Border Style by @damienfa in #2651
    • Word2007 Writer: Support for field REF by @crystoline in #2652
    • Word2007 Reader : Support for FormFields by @vincentKool in #2653
    • RTF Writer : Support for Table Border Style fixing #345 by @Progi1984 in #2656
    • Word2007 Reader: Support the page break () by @stanolacko in #2662
    • MsDoc Reader: Support for UTF-8 characters by @Progi1984 fixing #881, #1454, #1817, #1927, #2383, #2565 in #2664
    • Word2007 Writer: Added support for multiples comment for the same text by @rodrigoq fixing #2109 in #2665

    Bug fixes

    • MsDoc Reader : Correct Font Size Calculation by @oleibman fixing #2526 in #2531
    • Html Reader : Process Titles as Headings not Paragraphs @0b10011 and @oleibman Issue #1692 PR #2533
    • Generate Table Cell if Row Doesn't Have Any @oleibman fixing #2505 in #2516
    • TemplateProcessor Persist File After Destruct @oleibman fixing #2539 in #2545
    • TemplateProcessor Destructor Problem with Php7 @oleibman fixing #2548 in #2554
    • bug: TemplateProcessor fix multiline values @gimler fixing #268, #2323 and #2486 in #2522
    • 32-bit Problem in PasswordEncoder @oleibman fixing #2550 in #2551
    • Typo : Fix hardcoded macro chars in TemplateProcessor method @glafarge in #2618
    • XML Reader : Prevent fatal errors when opening corrupt files or "doc" files @mmcev106 in #2626
    • Documentation : Updated Comment element by @laminga in #2650
    • HTML Reader : Read width & height attributes in points fixing #2589 by @Progi1984 in #2654
    • Template Processor : Fixed bad naming of variables fixing #2586 by @Progi1984 in #2655
    • Word2007 Writer : Fix first footnote appearing as separator #2634 by @jacksleight in #2635
    • Template Processor : Fixed images with transparent backgrounds displaying a white background by @ElwynVdb in #2638
    • HTML Writer : Fixed rowspan for tables by @andomiell in #2659
    • Word2007 Writer : Fixed StrikeThrough property by @noec764 fixing #1722 & #1693 in #2661
    • HTML Reader : Fixed link without href by @asmundstavdahl fixing #1562 in #2663

    Miscellaneous

    • Bump dompdf/dompdf from 2.0.3 to 2.0.4 by @dependabot in #2530
    • Bump phpunit/phpunit from 9.6.13 to 9.6.14 by @dependabot in #2519
    • Bump mpdf/mpdf from 8.2.0 to 8.2.2 by @dependabot in #2518
    • Bump phpmd/phpmd from 2.14.1 to 2.15.0 by @dependabot in #2538
    • Bump phpunit/phpunit from 9.6.14 to 9.6.15 by @dependabot in #2537
    • Bump symfony/process from 5.4.28 to 5.4.34 by @dependabot in #2536
    • Allow rgb() when converting Html by @oleibman fixing #2508 in #2512
    • Improved Issue Template by @Progi1984 in #2609
    • Bump phpoffice/math from 0.1.0 to 0.2.0 by @Progi1984 fixing #2559 in #2645
    • Bump tecnickcom/tcpdf from 6.6.5 to 6.7.5 by @dependabot in #2646
    • Bump mpdf/mpdf from 8.2.2 to 8.2.4 by @dependabot in #2647
    • Bump phenx/php-svg-lib from 0.5.1 to 0.5.4 by @dependabot in #2649
    • Bump phpstan/phpstan-phpunit from 1.3.15 to 1.4.0 by @dependabot in #2648

    New Contributors

  3. 1.2.02023-12-28 · 2023-12-28 08:13 UTC
    查看更新说明

    Enhancements

    • Word2007 Reader/Writer : Added noWrap table cell property by @kernusr in #2359
    • HTML Reader : Support for font-variant: small-caps by @cambraca in #2117
    • Improved TextDirection for styling a cell by @terryzwt in #2429
    • Word2007 Reader : Added option to disable loading images by @aelliott1485 in #2450
    • HTML Writer : Added border-spacing to default styles for table by @kernusr in #2451
    • Word2007 Reader : Support for table cell borders and margins by @kernusr in #2454
    • PDF Writer : Add config for defining the default font by @MikeMaldini in #2262 & #2468
    • Word2007 Reader : Added support for Comments by @shaedrich in #2161 & #2469
    • Word2007 Reader/Writer: Permit book-fold printing by @potofcoffee in #2225 & #2470
    • Word2007 Writer : Add PageNumber to TOC by @jet-desk in #1652 & #2471
    • Word2007 Reader/Writer + ODText Reader/Writer : Add Element Formula in by @Progi1984 in #2477
    • Add Support for Various Missing Features in HTML Writer by @oleibman in #2475
      • Fixed addHTML (text-align:right in html is not handled correctly) in #2467
      • HTML Writer : Added ability to specify generic fallback font
      • HTML Writer : Added ability to specify handling of whitespace
      • HTML Writer : Added support for Table Border style, color, and size
      • HTML Writer : Added support for empty paragraphs (Word writer permits, browsers generally suppress)
      • HTML Writer : Paragraph style should support indentation, line-height, page-break-before
      • HTML Writer : Removed margin-top/bottom when spacing is null in Paragraph style
      • HTML Writer : Added default paragraph style to all paragraphs, as well as class Normal
      • HTML Writer : Use css @page and page declarations for sections
      • HTML Writer : Wrap sections in div, with page break before each (except first)
      • PDF Writer : Added support for PageBreak
      • PDF Writer : Added callback for modifying the HTML
      • Added Support for Language, both for document overall and individual text elements
    • Template : Set a checkbox by @nxtpge in #2509
    • ODText / RTF / Word2007 Writer : Add field FILENAME by @milkyway-git in #2510
    • ODText Reader : Improve Section Reader by @oleibman in #2507

    Bug fixes

    • Fixed wrong mimetype for docx files by @gamerlv in #2416
    • Word2007 Reader : Read hyperlingks in headings by @hannesdorn in #2433
    • PclZip : strtr using empty string by @spl1nes in #2432
    • Fixed PHP 8.2 deprecated about Allow access to an undefined property by @DAdq26 in #2440
    • Template Processor : Fixed choose dimention for Float Value by @gdevilbat in #2449
    • HTML Parser : Fix image parsing from url without extension by @JokubasR in #2459
    • Word2007 Reader : Fixed reading of Office365 DocX file by @filippotoso & @lfglopes in #2506
    • Word2007 Reader : Check for null on $fontDefaultStyle by @spatialfree in #2513

    Miscellaneous

    • Added PHPStan by @PowerKiKi in #2405
    • Bump symfony/process from 4.4.44 to 5.4.26 by @dependabot in #2431
    • Bump phpunit/phpunit from 9.6.8 to 9.6.10 by @dependabot in #2430
    • Added Coveralls.io by @Progi1984 in #2452
    • Added support for PHP 8.2 & PHP 8.3 by @Progi1984 in #2453
    • Moved documention from ReadTheDocs to MkDocs & Github Pages by @Progi1984 in #2465
    • Bump phpstan/phpstan-phpunit from 1.3.13 to 1.3.14 by @dependabot in #2457
    • Bump symfony/process from 5.4.26 to 5.4.28 by @dependabot in #2456
    • Bump phpunit/phpunit from 9.6.10 to 9.6.11 by @dependabot in #2455
    • Remove deprecated utf8_encode in PHP 8.2 by @mhcwebdesign in #2447 & #2472
    • Bump mpdf/mpdf from 8.1.6 to 8.2.0 by @dependabot in #2480
    • Bump phpunit/phpunit from 9.6.11 to 9.6.13 by @dependabot in #2481
    • Bump tecnickcom/tcpdf from 6.6.2 to 6.6.5 by @dependabot in #2482
    • Bump phpmd/phpmd from 2.13.0 to 2.14.1 by @dependabot in #2483
    • Bump phpstan/phpstan-phpunit from 1.3.14 to 1.3.15 by @dependabot in #2494

    BC Breaks

    • Removed dependency laminas/laminas-escaper

    New Contributors

    • @dependabot made
  4. 1.2.0-beta.1pre-release2023-11-30 · 2023-11-30 20:30 UTC
    查看更新说明

    Enhancements

    • Word2007 Reader/Writer : Added noWrap table cell property by @kernusr in #2359
    • HTML Reader : Support for font-variant: small-caps by @cambraca in #2117
    • Improved TextDirection for styling a cell by @terryzwt in #2429
    • Word2007 Reader : Added option to disable loading images by @aelliott1485 in #2450
    • HTML Writer : Added border-spacing to default styles for table by @kernusr in #2451
    • Word2007 Reader : Support for table cell borders and margins by @kernusr in #2454
    • PDF Writer : Add config for defining the default font by @MikeMaldini in #2262 & #2468
    • Word2007 Reader : Added support for Comments by @shaedrich in #2161 & #2469
    • Word2007 Reader/Writer: Permit book-fold printing by @potofcoffee in #2225 & #2470
    • Word2007 Writer : Add PageNumber to TOC by @jet-desk in #1652 & #2471
    • Word2007 Reader/Writer + ODText Reader/Writer : Add Element Formula in by @Progi1984 in #2477
    • Add Support for Various Missing Features in HTML Writer by @oleibman in #2475
      • Fixed addHTML (text-align:right in html is not handled correctly) in #2467
      • HTML Writer : Added ability to specify generic fallback font
      • HTML Writer : Added ability to specify handling of whitespace
      • HTML Writer : Added support for Table Border style, color, and size
      • HTML Writer : Added support for empty paragraphs (Word writer permits, browsers generally suppress)
      • HTML Writer : Paragraph style should support indentation, line-height, page-break-before
      • HTML Writer : Removed margin-top/bottom when spacing is null in Paragraph style
      • HTML Writer : Added default paragraph style to all paragraphs, as well as class Normal
      • HTML Writer : Use css @page and page declarations for sections
      • HTML Writer : Wrap sections in div, with page break before each (except first)
      • PDF Writer : Added support for PageBreak
      • PDF Writer : Added callback for modifying the HTML
      • Added Support for Language, both for document overall and individual text elements
    • Template : Set a checkbox by @nxtpge in #2509
    • ODText / RTF / Word2007 Writer : Add field FILENAME by @milkyway-git in #2510
    • ODText Reader : Improve Section Reader by @oleibman in #2507

    Bug fixes

    • Fixed wrong mimetype for docx files by @gamerlv in #2416
    • Word2007 Reader : Read hyperlingks in headings by @hannesdorn in #2433
    • PclZip : strtr using empty string by @spl1nes in #2432
    • Fixed PHP 8.2 deprecated about Allow access to an undefined property by @DAdq26 in #2440
    • Template Processor : Fixed choose dimention for Float Value by @gdevilbat in #2449
    • HTML Parser : Fix image parsing from url without extension by @JokubasR in #2459
    • Word2007 Reader : Fixed reading of Office365 DocX file by @filippotoso & @lfglopes in #2506
    • Word2007 Reader : Check for null on $fontDefaultStyle by @spatialfree in #2513

    Miscellaneous

    • Added PHPStan by @PowerKiKi in #2405
    • Bump symfony/process from 4.4.44 to 5.4.26 by @dependabot in #2431
    • Bump phpunit/phpunit from 9.6.8 to 9.6.10 by @dependabot in #2430
    • Added Coveralls.io by @Progi1984 in #2452
    • Added support for PHP 8.2 & PHP 8.3 by @Progi1984 in #2453
    • Moved documention from ReadTheDocs to MkDocs & Github Pages by @Progi1984 in #2465
    • Bump phpstan/phpstan-phpunit from 1.3.13 to 1.3.14 by @dependabot in #2457
    • Bump symfony/process from 5.4.26 to 5.4.28 by @dependabot in #2456
    • Bump phpunit/phpunit from 9.6.10 to 9.6.11 by @dependabot in #2455
    • Remove deprecated utf8_encode in PHP 8.2 by @mhcwebdesign in #2447 & #2472
    • Bump mpdf/mpdf from 8.1.6 to 8.2.0 by @dependabot in #2480
    • Bump phpunit/phpunit from 9.6.11 to 9.6.13 by @dependabot in #2481
    • Bump tecnickcom/tcpdf from 6.6.2 to 6.6.5 by @dependabot in #2482
    • Bump phpmd/phpmd from 2.13.0 to 2.14.1 by @dependabot in #2483
    • Bump phpstan/phpstan-phpunit from 1.3.14 to 1.3.15 by @dependabot in #2494

    BC Breaks

    • Removed dependency laminas/laminas-escaper

    New Contributors

    • @dependabot made
  5. 1.1.02023-05-30 · 2023-05-30 08:01 UTC
    查看更新说明

    Enhancements

    • Introduce deleteRow() method for TemplateProcessor
    • HTML Reader: Add basic support for CSS Style Tag
    • Allow customizing macro syntax in TemplateProcessor
    • Add background color support for textboxes
    • Add softhyphen support to word reader
    • Add support table row height when importing HTML
    • Add support for fractional font sizes
    • Added image quality support, with the maximum quality as default
    • Support for reading nested tables

    Bug fixes

    • DOCX reader: Read empty vmerge
    • Fixed setting width of Cell Style

    Miscellaneous

    • master is the new default branch

用户点评

登录用户的真实使用记录,可按版本和硬件筛选
共 0 条

    还没有人点评。第一个说出真实体验的人最有价值。

    README 原文

    未翻译

    PHPWord

    Latest Stable Version Coverage Status Total Downloads License

    Branch Master : PHPWord

    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 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 unreleased version

    composer require phpoffice/phpword:dev-master
    

    Getting started

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

    <?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.