使用 PHP 解析/编辑 docx 文件 [英] Parsing/edition docx file with PHP

查看:53
本文介绍了使用 PHP 解析/编辑 docx 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被要求编写一个 php 脚本,该脚本应该读取/解析 docx 文件并执行一些操作,例如复制特定段落/表格并用值填充一些变量(#myvar 或 $myvar).你们有什么建议,直接使用 word/document.xml 文件或将整个文档转换为 HTML 文件,然后使用 DOM 解析它(我不喜欢这个解决方案:()?要解析的 docx 的结构尚未定义,这是我的工作!并且必须尽可能通用.

I've been asked to write a php script that should read/parse a docx file and do some operations such as duplicate a specific paragraph/table and fill-in some variables (#myvar or $myvar) with values. What do you guys recommand, use the word/document.xml file directly or convert the whole document to an HTML file and then parse it using DOM(I don't like this solution :( )? the structure of the docx to parse is not defined yet, it's my job to do that ! And it has to be as general as possible.

为了清楚地了解我在做什么,docx 文件是一个 CV 模型,我必须用 DB 中的数据填充它.P.S:我不知道如何使用 Xquery 有效地解析/修改 XML 文件,因为我唯一的解决方案是在该 docx 中使用变量(带有 $ 或 #.. 的纯文本)谢谢你的帮助:)

To have a clear idea about what I'm doing, the docx file is a CV model that I have to fill-in with data from DB. P.S: I don't know how to efficiently parse/modify the XML file using Xquery since the only solution I have is to use variables (plain text with $ or #..) inside that docx thanks for your help :)

推荐答案

有 2 个主要的 PHP 库能够创建 Word 文档.以下是可能帮助您解决问题的两种功能的说明:

There are 2 major PHP libraries able to create Word documents. Here's a description of features from both that might help you solve your problem:

  1. PHPWord(开源) - 允许加载模板文档并替换值...看看这个图书馆源代码中的例子,也许你可以定义简历模板并使用它来制定解决方案;
  2. PHPDocX(基本功能免费,高级功能付费) - 允许模板和搜索和替换文档中的内容(可能只有付费版本).
  1. PHPWord (opensource) - allows to load template documents and replace values... take a look at this example in library's source code, maybe you can define a CV template and use this to work a solution out;
  2. PHPDocX (free with basic features, paid for more advanced features) - allows templates and search and replace of content in documents (probably only in paid versions though).

这篇关于使用 PHP 解析/编辑 docx 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆