docx 0.10.1 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +27 -3
- data/lib/docx/containers/paragraph.rb +1 -1
- data/lib/docx/document.rb +30 -1
- data/lib/docx/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 16df363293a0fcb4945e4ab7aa968f7138f2d0e38d25bcf689a5c5142279e520
|
|
4
|
+
data.tar.gz: '089d8ef9c78a7ae13d980d3a78ce729b05fe6dff1fefe8bbdfe4488a6b7b0f91'
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 8a23b154632578c3a702d3e16b6bbc3d763f062860ea9cf8db6c6814e89c1e7d99073fa28b7f90276c883d5c1f47be9d688d6f05177c06dbfc3bc452ccadc371
|
|
7
|
+
data.tar.gz: a8a735936caad5c75f56cace55e980df01bb25af795d5b8d040b735b14510898b220da3b8f40c52b74c68dc371c9a2d25bc2791d44a54a74dde9db727d3432ae
|
data/README.md
CHANGED
|
@@ -5,13 +5,13 @@
|
|
|
5
5
|
[](https://coveralls.io/github/ruby-docx/docx?branch=master)
|
|
6
6
|
[](https://gitter.im/ruby-docx/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
|
|
7
7
|
|
|
8
|
-
A ruby library/gem for interacting with `.docx` files. currently capabilities include reading paragraphs/bookmarks, inserting text at bookmarks, reading tables/rows/columns/cells and saving the document.
|
|
8
|
+
A ruby library/gem for interacting with `.docx` files. currently capabilities include reading paragraphs/bookmarks, inserting text at bookmarks, reading and writing headers/footers, reading tables/rows/columns/cells and saving the document.
|
|
9
9
|
|
|
10
10
|
## Usage
|
|
11
11
|
|
|
12
12
|
### Prerequisites
|
|
13
13
|
|
|
14
|
-
- Ruby 2.
|
|
14
|
+
- Ruby 2.7 or later
|
|
15
15
|
|
|
16
16
|
### Install
|
|
17
17
|
|
|
@@ -63,6 +63,26 @@ doc = Docx::Document.open(buffer)
|
|
|
63
63
|
# Everything about reading is the same as shown above
|
|
64
64
|
```
|
|
65
65
|
|
|
66
|
+
### Reading headers and footers
|
|
67
|
+
|
|
68
|
+
``` ruby
|
|
69
|
+
require 'docx'
|
|
70
|
+
|
|
71
|
+
doc = Docx::Document.open('example.docx')
|
|
72
|
+
|
|
73
|
+
# Headers and footers are returned as hashes keyed by their file name
|
|
74
|
+
# (e.g. "header1", "footer1"), with Nokogiri documents as values.
|
|
75
|
+
doc.headers.each do |name, header|
|
|
76
|
+
puts name
|
|
77
|
+
puts header.text
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
doc.footers.each do |name, footer|
|
|
81
|
+
puts name
|
|
82
|
+
puts footer.text
|
|
83
|
+
end
|
|
84
|
+
```
|
|
85
|
+
|
|
66
86
|
### Rendering html
|
|
67
87
|
``` ruby
|
|
68
88
|
require 'docx'
|
|
@@ -116,7 +136,11 @@ doc = Docx::Document.open('example.docx')
|
|
|
116
136
|
doc.bookmarks['example_bookmark'].insert_text_after("Hello world.")
|
|
117
137
|
|
|
118
138
|
# Insert multiple lines of text at our bookmark
|
|
119
|
-
doc.bookmarks['example_bookmark_2'].
|
|
139
|
+
doc.bookmarks['example_bookmark_2'].insert_multiple_lines(['Hello', 'World', 'foo'])
|
|
140
|
+
|
|
141
|
+
# Bookmarks placed in headers and footers are included too, and edits to them
|
|
142
|
+
# are saved along with the document.
|
|
143
|
+
doc.bookmarks['header_bookmark'].insert_text_after("Hello from the header.")
|
|
120
144
|
|
|
121
145
|
# Remove paragraphs
|
|
122
146
|
doc.paragraphs.each do |p|
|
data/lib/docx/document.rb
CHANGED
|
@@ -22,7 +22,7 @@ module Docx
|
|
|
22
22
|
class Document
|
|
23
23
|
include Docx::SimpleInspect
|
|
24
24
|
|
|
25
|
-
attr_reader :xml, :doc, :zip, :styles
|
|
25
|
+
attr_reader :xml, :doc, :zip, :styles, :headers, :footers
|
|
26
26
|
|
|
27
27
|
def initialize(path_or_io, options = {})
|
|
28
28
|
@replace = {}
|
|
@@ -40,6 +40,8 @@ module Docx
|
|
|
40
40
|
@document_xml = document.get_input_stream.read
|
|
41
41
|
@doc = Nokogiri::XML(@document_xml)
|
|
42
42
|
load_styles
|
|
43
|
+
load_headers
|
|
44
|
+
load_footers
|
|
43
45
|
yield(self) if block_given?
|
|
44
46
|
ensure
|
|
45
47
|
@zip.close unless @zip.nil?
|
|
@@ -68,6 +70,9 @@ module Docx
|
|
|
68
70
|
def bookmarks
|
|
69
71
|
bkmrks_hsh = {}
|
|
70
72
|
bkmrks_ary = @doc.xpath('//w:bookmarkStart').map { |b_node| parse_bookmark_from b_node }
|
|
73
|
+
# also scan headers and footers so their bookmarks can be read and edited
|
|
74
|
+
bkmrks_ary += headers.values.flat_map { |h| h.xpath('//w:bookmarkStart').map { |b_node| parse_bookmark_from b_node } }
|
|
75
|
+
bkmrks_ary += footers.values.flat_map { |f| f.xpath('//w:bookmarkStart').map { |b_node| parse_bookmark_from b_node } }
|
|
71
76
|
# auto-generated by office 2010
|
|
72
77
|
bkmrks_ary.reject! { |b| b.name == '_GoBack' }
|
|
73
78
|
bkmrks_ary.each { |b| bkmrks_hsh[b.name] = b }
|
|
@@ -200,6 +205,24 @@ module Docx
|
|
|
200
205
|
Zip.write_zip64_support = previous
|
|
201
206
|
end
|
|
202
207
|
|
|
208
|
+
def load_headers
|
|
209
|
+
header_files = @zip.glob("word/header*.xml").map{|h| h.name}
|
|
210
|
+
filename_and_contents_pairs = header_files.map do |file|
|
|
211
|
+
simple_file_name = file.sub(/^word\//, "").sub(/\.xml$/, "")
|
|
212
|
+
[simple_file_name, Nokogiri::XML(@zip.read(file))]
|
|
213
|
+
end
|
|
214
|
+
@headers = Hash[filename_and_contents_pairs]
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
def load_footers
|
|
218
|
+
footer_files = @zip.glob("word/footer*.xml").map{|h| h.name}
|
|
219
|
+
filename_and_contents_pairs = footer_files.map do |file|
|
|
220
|
+
simple_file_name = file.sub(/^word\//, "").sub(/\.xml$/, "")
|
|
221
|
+
[simple_file_name, Nokogiri::XML(@zip.read(file))]
|
|
222
|
+
end
|
|
223
|
+
@footers = Hash[filename_and_contents_pairs]
|
|
224
|
+
end
|
|
225
|
+
|
|
203
226
|
def load_styles
|
|
204
227
|
@styles_xml = @zip.read('word/styles.xml')
|
|
205
228
|
@styles = Nokogiri::XML(@styles_xml)
|
|
@@ -225,6 +248,12 @@ module Docx
|
|
|
225
248
|
def update
|
|
226
249
|
replace_entry 'word/document.xml', doc.serialize(save_with: 0)
|
|
227
250
|
replace_entry 'word/styles.xml', styles_configuration.serialize(save_with: 0)
|
|
251
|
+
headers.each do |name, content|
|
|
252
|
+
replace_entry "word/#{name}.xml", content.serialize(save_with: 0)
|
|
253
|
+
end
|
|
254
|
+
footers.each do |name, content|
|
|
255
|
+
replace_entry "word/#{name}.xml", content.serialize(save_with: 0)
|
|
256
|
+
end
|
|
228
257
|
end
|
|
229
258
|
|
|
230
259
|
# generate Elements::Containers::Paragraph from paragraph XML node
|
data/lib/docx/version.rb
CHANGED