From 548e42c3d6b434c064351d7280cda51765dbe22f Mon Sep 17 00:00:00 2001 From: Jay Hemnani <193022578+jayhemnani9910@users.noreply.github.com> Date: Tue, 17 Nov 2026 01:38:33 +0530 Subject: [PATCH] Save edits to the main part it was read from, not always document.xml --- lib/docx/document.rb | 3 ++- spec/integration/editing_spec.rb | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/docx/document.rb b/lib/docx/document.rb index 7900ad0..18e1c48 100755 --- a/lib/docx/document.rb +++ b/lib/docx/document.rb @@ -40,6 +40,7 @@ def initialize(path_or_io, options = {}) document = @zip.glob('word/document*.xml').first raise Errno::ENOENT if document.nil? + @document_path = document.name @document_xml = document.get_input_stream.read @doc = Nokogiri::XML(@document_xml) load_styles @@ -261,7 +262,7 @@ def load_rels # end of methods that make edits? #++ def update - replace_entry 'word/document.xml', doc.serialize(save_with: 0) + replace_entry @document_path, doc.serialize(save_with: 0) replace_entry 'word/styles.xml', styles_configuration.serialize(save_with: 0) headers.each do |name, content| replace_entry "word/#{name}.xml", content.serialize(save_with: 0) diff --git a/spec/integration/editing_spec.rb b/spec/integration/editing_spec.rb index c5295e8..0bf30bb 100644 --- a/spec/integration/editing_spec.rb +++ b/spec/integration/editing_spec.rb @@ -107,6 +107,18 @@ end end + # Word for the web names the main part word/document2.xml. + describe 'office365.docx' do + it 'persists edits to a main part not named document.xml' do + doc = Docx::Document.open(fixture_path('office365.docx')) + + doc.paragraphs.first.text = 'edited' + doc.save(tmp_path('saved.docx')) + + expect(Docx::Document.open(tmp_path('saved.docx')).paragraphs.first.text).to eq('edited') + end + end + describe 'multi_doc_bookmarks.docx' do it 'edits bookmarks in headers and footers and persists them' do doc = Docx::Document.open(fixture_path('multi_doc_bookmarks.docx'))