Skip to content

Commit ee136a9

Browse files
satoryuclaudeJawad79Ahmad
committed
Add ability to read document footers
Adds a Docx::Document#footers accessor that exposes word/footer*.xml as a Hash keyed by footer file name, mirroring the headers reading feature (#173). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Co-authored-by: Jawad Ahmad <Jawad79Ahmad@users.noreply.github.com>
1 parent c39e9c8 commit ee136a9

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

lib/docx/document.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ module Docx
2222
class Document
2323
include Docx::SimpleInspect
2424

25-
attr_reader :xml, :doc, :zip, :styles, :headers
25+
attr_reader :xml, :doc, :zip, :styles, :headers, :footers
2626

2727
def initialize(path_or_io, options = {})
2828
@replace = {}
@@ -41,6 +41,7 @@ def initialize(path_or_io, options = {})
4141
@doc = Nokogiri::XML(@document_xml)
4242
load_styles
4343
load_headers
44+
load_footers
4445
yield(self) if block_given?
4546
ensure
4647
@zip.close unless @zip.nil?
@@ -210,6 +211,15 @@ def load_headers
210211
@headers = Hash[filename_and_contents_pairs]
211212
end
212213

214+
def load_footers
215+
footer_files = @zip.glob("word/footer*.xml").map{|h| h.name}
216+
filename_and_contents_pairs = footer_files.map do |file|
217+
simple_file_name = file.sub(/^word\//, "").sub(/\.xml$/, "")
218+
[simple_file_name, Nokogiri::XML(@zip.read(file))]
219+
end
220+
@footers = Hash[filename_and_contents_pairs]
221+
end
222+
213223
def load_styles
214224
@styles_xml = @zip.read('word/styles.xml')
215225
@styles = Nokogiri::XML(@styles_xml)

spec/docx/document_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,18 @@
7272
end
7373
end
7474

75+
describe 'read footers' do
76+
before do
77+
@doc = Docx::Document.open(@fixtures_path + '/multi_doc.docx')
78+
end
79+
80+
it 'can extract footers' do
81+
expect(@doc.footers).to_not be_nil
82+
expect(@doc.footers.keys).to eq ["footer1"]
83+
expect(@doc.footers["footer1"].text).to eq "Hello from the footer."
84+
end
85+
end
86+
7587
describe 'read tables' do
7688
before do
7789
@doc = Docx::Document.open(@fixtures_path + '/tables.docx')

0 commit comments

Comments
 (0)