在 Ruby 中创建 Microsoft Word (.docx) 文档 [英] Creating Microsoft Word (.docx) documents in Ruby

查看:33
本文介绍了在 Ruby 中创建 Microsoft Word (.docx) 文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种在 Ruby 应用程序中创建 Word 文档 (.docx) 的简单方法?实际上,就我而言,它是从 Linux 服务器提供服务的 Rails 应用程序.

Is there an easy way to create Word documents (.docx) in a Ruby application? Actually, in my case it's a Rails application served from a Linux server.

类似于 Prawn 的宝石,但用于 DOCX 而不是 PDF 会很棒!

A gem similar to Prawn but for DOCX instead of PDF would be great!

推荐答案

如前所述,似乎没有任何库可以在 Ruby 中操作 Open XML 文档,但是 OpenXML Developer 拥有关于 Open XML 文档格式的完整文档.

As has been noted, there don't appear to be any libraries to manipulate Open XML documents in Ruby, but OpenXML Developer has complete documentation on the format of Open XML documents.

如果您想要发送为每个用户定制的标准文档(如套用信函)的副本,考虑到 DOCX 是包含目录层次结构中各个部分的 ZIP 文件,这应该相当简单.拥有一个 DOCX模板",其中包含您要发送给 all 用户的所有部分和树结构(没有实际内容),然后只需创建包含用户的新(或修改现有)部分- 您需要的特定内容并将其注入 ZIP(DOCX 文件)中,然后再将其发送给用户.

If what you want is to send a copy of a standard document (like a form letter) customized for each user, it should be fairly simple given that a DOCX is a ZIP file that contains various parts in a directory hierarchy. Have a DOCX "template" that contains all the parts and tree structure that you want to send to all users (with no real content), then simply create new (or modify existing) pieces that contain the user-specific content you want and inject it into the ZIP (DOCX file) before sending it to the user.

例如:您可以拥有包含亲爱的 [USER-PLACEHOLDER]:document-template.xml.当用户请求文档时,您将 [USER-PLACEHOLDER] 替换为用户名,然后将生成的 document.xml 添加到 your-template.docx ZIP 文件(其中包含 Word 文档中所需的所有图像和其他部分)并将生成的文档发送给用户.

For example: You could have document-template.xml that contains Dear [USER-PLACEHOLDER]:. When a user requests the document, you replace [USER-PLACEHOLDER] with the user's name, then add the resulting document.xml to the your-template.docx ZIP file (which would contain all the images and other parts you want in the Word document) and send that resulting document to the user.

请注意,如果您将 .docx 文件重命名为 .zip,探索其中部分的结构和格式就很简单了.您可以使用任何 ZIP 操作工具或以编程方式使用代码非常轻松地删除或替换图像或其他部分.

Note that if you rename a .docx file to .zip it is trivial to explore the structure and format of the parts inside. You can remove or replace images or other parts very easily with any ZIP manipulation tools or programmatically with code.

如果不访问 API 来简化工作,那么从原始 XML 生成具有完全自定义内容的全新 Word 文档将非常困难.如果你真的需要这样做,你可以考虑安装 Mono,然后使用 VB.NET、C#或 IronRuby 使用 开放 XML 格式 SDK 1.0.因为您只是使用 Microsoft.Office.DocumentFormat.OpenXml.Packaging Namespace 用于操作 Open XML 文档,它在 Mono 中应该可以正常工作,它似乎支持 SDK 所需的一切.

Generating a brand new Word document with completely custom content from raw XML would be very difficult without access to an API to make the job easier. If you really need to do that, you might consider installing Mono, then use VB.NET, C# or IronRuby to create your Open XML documents using the Open XML Format SDK 1.0. Since you would just be using the Microsoft.Office.DocumentFormat.OpenXml.Packaging Namespace to manipulate Open XML documents, it should work okay in Mono, which seems to support everything the SDK requires.

这篇关于在 Ruby 中创建 Microsoft Word (.docx) 文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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