Class: MsDocx::Document
- Inherits:
-
Object
- Object
- MsDocx::Document
- Defined in:
- lib/ms_docx/document.rb
Overview
Constant Summary collapse
- CONTENT_ENTRY =
'word/document.xml'
Class Method Summary collapse
-
.open(file_path) ⇒ MsDocx::Document
Open .docx file.
Instance Method Summary collapse
- #each_paragraph(&block) ⇒ Object
-
#initialize(file_path, *args) ⇒ Document
constructor
A new instance of Document.
- #paragraphs ⇒ Object
- #save_file(file_path) ⇒ Object
- #to_text ⇒ Object
- #to_xml ⇒ Object
Constructor Details
#initialize(file_path, *args) ⇒ Document
Returns a new instance of Document.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/ms_docx/document.rb', line 24 def initialize(file_path, *args) if File.exist? file_path @file_path = file_path @temp_file_path = file_path.sub(/\.docx/, '~temp.docx') @docx_zip = Zip::File.open(file_path) else raise MsDocx::Errors::FileNotExist, "File '#{file_path}' doesn't exist ❗" end @doc = @docx_zip.find_entry(CONTENT_ENTRY).get_input_stream.read @xml = Nokogiri::XML::Document.parse @doc @paragraphs = [] @xml.xpath('//w:body//w:p').each do |w_p| @paragraphs << MsDocx::Paragraph.new(w_p) end # @xml.xpath('//w:p//w:r//w:t').each do |p| # # # puts p.content # # # p.content = 'test' # # # end # # # # File.open(new_file, 'wb') {|f| f.write(buffer.string) } end |
Class Method Details
.open(file_path) ⇒ MsDocx::Document
Open .docx file
18 19 20 |
# File 'lib/ms_docx/document.rb', line 18 def open(file_path) MsDocx::Document.new(file_path) end |
Instance Method Details
#each_paragraph(&block) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/ms_docx/document.rb', line 65 def each_paragraph(&block) @paragraphs.each do |paragraph| block.call(paragraph) end # block.call(self) end |
#paragraphs ⇒ Object
61 62 63 |
# File 'lib/ms_docx/document.rb', line 61 def paragraphs @paragraphs end |
#save_file(file_path) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/ms_docx/document.rb', line 99 def save_file(file_path) @buffer = Zip::OutputStream.write_buffer do |out| @docx_zip.entries.each do |e| if e.name == CONTENT_ENTRY # doc = get_xml_doc_from_stream(e.get_input_stream.read) # doc.xpath('//w:body//w:p').each do |pt| # pt.xpath(pt.path + '//w:t'). # p pt.to_xml # block.call(pt) # p pt.to_xml # w_r_list = pt.xpath(pt.path + '//w:r') # if w_r_list.length > 1 # w_r_list[1..-1].each{|pp| pp.remove } # p pt.to_xml # p w_r_list.to_xml # end # pt # p w_r_list.to_xml # w_r_list.first.xpath(w_r_list.first.path + '//w:t').map{|pp| block.call(pp)} if w_r_list.first # p nil # p nil # p nil # end # p doc.to_xml out.put_next_entry(CONTENT_ENTRY) out.write self.to_xml else out.put_next_entry(e.name) e.write_to_zip_output_stream out end end end File.open(file_path, 'wb') {|f| f.write(@buffer.string) } end |
#to_text ⇒ Object
80 81 82 |
# File 'lib/ms_docx/document.rb', line 80 def to_text @paragraphs.map(&:text).join("\n") end |
#to_xml ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/ms_docx/document.rb', line 84 def to_xml body = @xml.at('//w:body') body.children = '' @paragraphs.each do |paragraph| body.add_child(paragraph.to_xml) end @xml.at('//w:document').children = body @xml.to_xml end |