【发布时间】:2012-02-22 21:27:11
【问题描述】:
在这里,我使用 rubyzip 和 nokogiri 来修改 .docx 文件。
RubyZip -> Unzip .docx file
Nokogiri -> Parse and change in content of the body of word/document.xml
当我在下面编写示例代码时,代码修改了文件,但其他文件受到干扰。换句话说,更新的文件没有打开显示错误,字处理器崩溃。我该如何解决这个问题?
require 'zip/zipfilesystem'
require 'nokogiri'
zip = Zip::ZipFile.open("SecurityForms.docx")
doc = zip.find_entry("word/document.xml")
xml = Nokogiri::XML.parse(doc.get_input_stream)
wt = xml.root.xpath("//w:t", {"w" => "http://schemas.openxmlformats.org/wordprocessingml/2006/main"}).first
wt.content = "FinalStatement"
zip.get_output_stream("word/document.xml") {|f| f << xml.to_s}
zip.close
【问题讨论】:
标签: ruby zip nokogiri replace document-management