在 Ruby 中创建 Microsoft Word (.docx) 文档
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/697505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating Microsoft Word (.docx) documents in Ruby
提问by Javier
采纳答案by Grant Wagner
As has been noted, there don't appear to be any libraries to manipulate Open XML documents in Ruby, but OpenXML Developerhas complete documentation on the format of Open XML documents.
如前所述,似乎没有任何库可以在 Ruby 中操作 Open XML 文档,但OpenXML Developer有关于 Open XML 文档格式的完整文档。
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 allusers (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.
如果您想要发送为每个用户定制的标准文档(如套用信函)的副本,考虑到 DOCX 是包含目录层次结构中各个部分的 ZIP 文件,这应该相当简单。拥有一个 DOCX“模板”,其中包含您要发送给all用户的所有部分和树结构(没有实际内容),然后只需创建包含您想要的用户特定内容的新(或修改现有)部分并注入它在将其发送给用户之前进入 ZIP(DOCX 文件)。
For example: You could have document-template.xmlthat contains Dear [USER-PLACEHOLDER]:. When a user requests the document, you replace [USER-PLACEHOLDER]with the user's name, then add the resulting document.xmlto the your-template.docxZIP file (which would contain all the images and other parts you want in the Word document) and send that resulting document to the user.
例如:您可以拥有document-template.xml包含Dear [USER-PLACEHOLDER]: 的内容。当用户请求文档时,您将替换[USER-PLACEHOLDER]为用户名,然后将结果添加document.xml到your-template.docxZIP 文件(其中包含 Word 文档中所需的所有图像和其他部分)并将生成的文档发送给用户。
Note that if you rename a .docxfile to .zipit 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.
请注意,如果您将一个.docx文件重命名为.zip它,则探索内部部件的结构和格式是微不足道的。您可以使用任何 ZIP 操作工具或以编程方式使用代码非常轻松地删除或替换图像或其他部分。
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 IronRubyto 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 Namespaceto manipulate Open XML documents, it should work okay in Mono, which seems to support everything the SDK requires.
如果不访问 API 来简化工作,那么从原始 XML 生成具有完全自定义内容的全新 Word 文档将非常困难。如果您确实需要这样做,您可以考虑安装Mono,然后使用 VB.NET、C# 或IronRuby使用Open XML Format SDK 1.0创建您的 Open XML 文档。由于您只是使用Microsoft.Office.DocumentFormat.OpenXml.Packaging 命名空间来操作 Open XML 文档,因此它在 Mono 中应该可以正常工作,它似乎支持 SDK 所需的一切。
回答by ykaganovich
You can use Apache POI. It is written in Java, but integrates with Ruby as an extension
您可以使用Apache POI。它是用 Java 编写的,但作为扩展与 Ruby 集成
回答by Archonic
This is an old question but there's a new answer. If you'd like to turn an HTML doc into a Word (docx) doc, just use the 'htmltoword' gem:
这是一个老问题,但有一个新答案。如果您想将 HTML 文档转换为 Word (docx) 文档,只需使用 'htmltoword' gem:
https://github.com/karnov/htmltoword
https://github.com/karnov/htmltoword
I'm not sure why there was answer creep and everyone started posting templating solutions, but this answers the OP's question. Just like Prawn, except Word instead of PDF.
我不确定为什么会有答案蔓延,每个人都开始发布模板解决方案,但这回答了 OP 的问题。就像 Prawn 一样,除了 Word 而不是 PDF。
UPDATE:
更新:
There's also pandocand an API wrapper for pandoc called docverter. Both have slightly complicated installs since pandoc is a haskell library.
还有pandoc和pandoc的 API 包装器,称为docverter。由于 pandoc 是一个 Haskell 库,因此两者的安装都有些复杂。
回答by Théo Capdet
Maybe this gem is interesting for you.
也许这颗宝石对你很有趣。
https://github.com/trade-informatics/caracal/
https://github.com/trade-informatics/caracal/
It like prawn but with docx.
它像对虾,但带有 docx。
回答by Daniel A. White
I know if you serve a HTML document as a word document with the .doc extension, it will open in Word just fine. Just don't do anything fancy.
我知道如果您将 HTML 文档作为扩展名为 .doc 的 Word 文档提供,它会在 Word 中正常打开。只是不要做任何花哨的事情。
Edit:Here is an example using classic ASP. http://www.aspdev.org/asp/asp-export-word/
编辑:这是一个使用经典 ASP 的示例。http://www.aspdev.org/asp/asp-export-word/
回答by Michael Cindric
This is a way Doccy (doccyapp.com) has a api that does just that which you can use. Supports docx, odt and pages and converts to PDF as well if you like
这是 Doccy ( doccyapp.com) 有一个 api 的一种方式,它可以执行您可以使用的操作。支持 docx、odt 和页面,如果您愿意,也可以转换为 PDF
回答by Nick
Using a technique very similar to that suggested by Grant Wagner I have created a Ruby html to word gem that should allow you to easily output Word docx files from your ruby app. You can check it out at http://github.com/nickfrandsen/htmltoword- Simply pass it a html string and it will create a corresponding word docx file.
使用与 Grant Wagner 建议的技术非常相似的技术,我创建了一个 Ruby html 到 word gem,它应该允许您轻松地从您的 ruby 应用程序输出 Word docx 文件。您可以在http://github.com/nickfrandsen/htmltoword 上查看- 只需向它传递一个 html 字符串,它就会创建一个相应的 word docx 文件。
def show
respond_to do |format|
format.docx do
file = Htmltoword::Document.create params[:docx_html_source], "file_name.docx"
send_file file.path, :disposition => "attachment"
end
end
end
Hope you find it useful. If you have any problems with it feel free to open a github issue.
希望你觉得它有用。如果您有任何问题,请随时打开 github 问题。
回答by edi9999
Disclosure: I'm the leader of the docxtemplater project.
披露:我是 docxtemplater 项目的负责人。
I know you're looking for a ruby solution, but because all other solutions only tell you how to do it globally, without giving you a library that does exactly what you want, here's a solution based on JS or NodeJS (works in both)
我知道您正在寻找 ruby 解决方案,但是因为所有其他解决方案只告诉您如何在全局范围内执行此操作,而没有为您提供完全符合您要求的库,这里有一个基于 JS 或 NodeJS 的解决方案(适用于两者)
You can also use it in the commandline:
您也可以在命令行中使用它:
npm install docxtemplater -g
docxtemplater <configFile>
----config.docxFile: The input file in docx format
----config.outputFile: The outputfile of the document
回答by JasonPlutext
Further to Grant's answer, you can also send Word a "Flat OPC" file, which is essentially the docx unzipped and concatenated to create a single xml file. This way, you can replace [USER-PLACEHOLDER] in one file and be done with it (ie no zipping or unzipping).
除了格兰特的回答,您还可以向 Word 发送“Flat OPC”文件,该文件本质上是解压缩并连接的 docx 以创建单个 xml 文件。这样,您可以在一个文件中替换 [USER-PLACEHOLDER] 并完成它(即不压缩或解压缩)。
回答by Amala
If anyone is still looking at this, this post explains how to use an XML data source. This works nicely for me.
如果有人还在看这个,这篇文章解释了如何使用 XML 数据源。这对我来说很好用。
http://seroter.wordpress.com/2009/12/23/populating-word-2007-templates-through-open-xml/
http://seroter.wordpress.com/2009/12/23/populating-word-2007-templates-through-open-xml/

