docx 0.2.07 → 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 2d330b3e0314db11795c19864a96cfe839e9c9959211f0b1b54af5270f4157c4
4
+ data.tar.gz: 291e34e45c7d56185cdf5716fc15e9143b2134bea6d14102cf699e0d3cd6f8f5
5
+ SHA512:
6
+ metadata.gz: cbbe86dd086c4cae691af852e596d7a416e6e08fac56ad76ba03e5d44b12285eb84391204afe3df4d53b4a2a760149a791a03efc4cc08942565d4cead0014d62
7
+ data.tar.gz: 107a8e8aaad25115b824812681335fb072aebad3945e3263b622105b6847c6c25abb30ca4c5cbd5753ea1b3940f27a91e6a82dc3fb7f126dcf465e18e314ed32
data/LICENSE.md CHANGED
@@ -1,21 +1,21 @@
1
- The MIT License
2
-
3
- Copyright (c) Marcus Ortiz, http://marcusortiz.com
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in
13
- all copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- THE SOFTWARE.
1
+ The MIT License
2
+
3
+ Copyright (c) Marcus Ortiz, http://marcusortiz.com
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md CHANGED
@@ -1,14 +1,37 @@
1
1
  # docx
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/docx.svg)](https://badge.fury.io/rb/docx)
4
+ [![Ruby](https://github.com/ruby-docx/docx/workflows/Ruby/badge.svg)](https://github.com/ruby-docx/docx/actions?query=workflow%3ARuby)
5
+ [![Coverage Status](https://coveralls.io/repos/github/ruby-docx/docx/badge.svg?branch=master)](https://coveralls.io/github/ruby-docx/docx?branch=master)
6
+ [![Gitter](https://badges.gitter.im/ruby-docx/community.svg)](https://gitter.im/ruby-docx/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
7
+
3
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.
4
9
 
5
10
  ## Usage
6
11
 
12
+ ### Prerequisites
13
+
14
+ - Ruby 2.5 or later
15
+
7
16
  ### Install
8
17
 
9
- Requires ruby (tested with 2.1.1)
18
+ Add the following line to your application's Gemfile:
19
+
20
+ ```ruby
21
+ gem 'docx'
22
+ ```
23
+
24
+ And then execute:
25
+
26
+ ```shell
27
+ bundle install
28
+ ```
10
29
 
11
- gem 'docx', '~> 0.2.07', :require => ["docx"]
30
+ Or install it yourself as:
31
+
32
+ ```shell
33
+ gem install docx
34
+ ```
12
35
 
13
36
  ### Reading
14
37
 
@@ -29,6 +52,17 @@ doc.bookmarks.each_pair do |bookmark_name, bookmark_object|
29
52
  end
30
53
  ```
31
54
 
55
+ Don't have a local file but a buffer? Docx handles those to:
56
+
57
+ ```ruby
58
+ require 'docx'
59
+
60
+ # Create a Docx::Document object from a remote file
61
+ doc = Docx::Document.open(buffer)
62
+
63
+ # Everything about reading is the same as shown above
64
+ ```
65
+
32
66
  ### Rendering html
33
67
  ``` ruby
34
68
  require 'docx'
@@ -61,7 +95,7 @@ doc.tables.each do |table|
61
95
  puts cell.text
62
96
  end
63
97
  end
64
-
98
+
65
99
  table.columns.each do |column| # Column-based iteration
66
100
  column.cells.each do |cell|
67
101
  puts cell.text
@@ -84,10 +118,51 @@ doc.bookmarks['example_bookmark'].insert_text_after("Hello world.")
84
118
  # Insert multiple lines of text at our bookmark
85
119
  doc.bookmarks['example_bookmark_2'].insert_multiple_lines_after(['Hello', 'World', 'foo'])
86
120
 
121
+ # Remove paragraphs
122
+ doc.paragraphs.each do |p|
123
+ p.remove! if p.to_s =~ /TODO/
124
+ end
125
+
126
+ # Substitute text, preserving formatting
127
+ doc.paragraphs.each do |p|
128
+ p.each_text_run do |tr|
129
+ tr.substitute('_placeholder_', 'replacement value')
130
+ end
131
+ end
132
+
87
133
  # Save document to specified path
88
134
  doc.save('example-edited.docx')
89
135
  ```
90
136
 
137
+ ### Writing to tables
138
+
139
+ ``` ruby
140
+ require 'docx'
141
+
142
+ # Create a Docx::Document object for our existing docx file
143
+ doc = Docx::Document.open('tables.docx')
144
+
145
+ # Iterate over each table
146
+ doc.tables.each do |table|
147
+ last_row = table.rows.last
148
+
149
+ # Copy last row and insert a new one before last row
150
+ new_row = last_row.copy
151
+ new_row.insert_before(last_row)
152
+
153
+ # Substitute text in each cell of this new row
154
+ new_row.cells.each do |cell|
155
+ cell.paragraphs.each do |paragraph|
156
+ paragraph.each_text_run do |text|
157
+ text.substitute('_placeholder_', 'replacement value')
158
+ end
159
+ end
160
+ end
161
+ end
162
+
163
+ doc.save('tables-edited.docx')
164
+ ```
165
+
91
166
  ### Advanced
92
167
 
93
168
  ``` ruby
@@ -1,4 +1,4 @@
1
- require 'docx/containers/container'
2
- require 'docx/containers/text_run'
3
- require 'docx/containers/paragraph'
4
- require 'docx/containers/table'
1
+ require 'docx/containers/container'
2
+ require 'docx/containers/text_run'
3
+ require 'docx/containers/paragraph'
4
+ require 'docx/containers/table'
@@ -14,7 +14,11 @@ module Docx
14
14
  def blank!
15
15
  @node.xpath(".//w:t").each {|t| t.content = '' }
16
16
  end
17
+
18
+ def remove!
19
+ @node.remove
20
+ end
17
21
  end
18
22
  end
19
23
  end
20
- end
24
+ end
@@ -55,7 +55,7 @@ module Docx
55
55
 
56
56
  # Array of text runs contained within paragraph
57
57
  def text_runs
58
- @node.xpath('w:r|w:hyperlink/w:r').map { |r_node| Containers::TextRun.new(r_node, @document_properties) }
58
+ @node.xpath('w:r|w:hyperlink').map { |r_node| Containers::TextRun.new(r_node, @document_properties) }
59
59
  end
60
60
 
61
61
  # Iterate over each text run within a paragraph
@@ -1,51 +1,51 @@
1
- require 'docx/containers/table_row'
2
- require 'docx/containers/table_column'
3
- require 'docx/containers/container'
4
-
5
- module Docx
6
- module Elements
7
- module Containers
8
- class Table
9
- include Container
10
- include Elements::Element
11
-
12
- def self.tag
13
- 'tbl'
14
- end
15
-
16
- def initialize(node)
17
- @node = node
18
- @properties_tag = 'tblGrid'
19
- end
20
-
21
- # Array of row
22
- def rows
23
- @node.xpath('w:tr').map {|r_node| Containers::TableRow.new(r_node) }
24
- end
25
-
26
- def row_count
27
- @node.xpath('w:tr').count
28
- end
29
-
30
- # Array of column
31
- def columns
32
- columns_containers = []
33
- (0..(column_count-1)).each do |i|
34
- columns_containers[i] = Containers::TableColumn.new @node.xpath("w:tr//w:tc[#{i+1}]")
35
- end
36
- columns_containers
37
- end
38
-
39
- def column_count
40
- @node.xpath('w:tblGrid/w:gridCol').count
41
- end
42
-
43
- # Iterate over each row within a table
44
- def each_rows
45
- rows.each { |r| yield(r) }
46
- end
47
-
48
- end
49
- end
50
- end
51
- end
1
+ require 'docx/containers/table_row'
2
+ require 'docx/containers/table_column'
3
+ require 'docx/containers/container'
4
+
5
+ module Docx
6
+ module Elements
7
+ module Containers
8
+ class Table
9
+ include Container
10
+ include Elements::Element
11
+
12
+ def self.tag
13
+ 'tbl'
14
+ end
15
+
16
+ def initialize(node)
17
+ @node = node
18
+ @properties_tag = 'tblGrid'
19
+ end
20
+
21
+ # Array of row
22
+ def rows
23
+ @node.xpath('w:tr').map {|r_node| Containers::TableRow.new(r_node) }
24
+ end
25
+
26
+ def row_count
27
+ @node.xpath('w:tr').count
28
+ end
29
+
30
+ # Array of column
31
+ def columns
32
+ columns_containers = []
33
+ (0..(column_count-1)).each do |i|
34
+ columns_containers[i] = Containers::TableColumn.new @node.xpath("w:tr//w:tc[#{i+1}]")
35
+ end
36
+ columns_containers
37
+ end
38
+
39
+ def column_count
40
+ @node.xpath('w:tblGrid/w:gridCol').count
41
+ end
42
+
43
+ # Iterate over each row within a table
44
+ def each_rows
45
+ rows.each { |r| yield(r) }
46
+ end
47
+
48
+ end
49
+ end
50
+ end
51
+ end
@@ -1,39 +1,39 @@
1
- require 'docx/containers/text_run'
2
- require 'docx/containers/container'
3
-
4
- module Docx
5
- module Elements
6
- module Containers
7
- class TableCell
8
- include Container
9
- include Elements::Element
10
-
11
- def self.tag
12
- 'tc'
13
- end
14
-
15
- def initialize(node)
16
- @node = node
17
- @properties_tag = 'tcPr'
18
- end
19
-
20
- # Return text of paragraph's cell
21
- def to_s
22
- paragraphs.map(&:text).join('')
23
- end
24
-
25
- # Array of paragraphs contained within cell
26
- def paragraphs
27
- @node.xpath('w:p').map {|p_node| Containers::Paragraph.new(p_node) }
28
- end
29
-
30
- # Iterate over each text run within a paragraph's cell
31
- def each_paragraph
32
- paragraphs.each { |tr| yield(tr) }
33
- end
34
-
35
- alias_method :text, :to_s
36
- end
37
- end
38
- end
39
- end
1
+ require 'docx/containers/text_run'
2
+ require 'docx/containers/container'
3
+
4
+ module Docx
5
+ module Elements
6
+ module Containers
7
+ class TableCell
8
+ include Container
9
+ include Elements::Element
10
+
11
+ def self.tag
12
+ 'tc'
13
+ end
14
+
15
+ def initialize(node)
16
+ @node = node
17
+ @properties_tag = 'tcPr'
18
+ end
19
+
20
+ # Return text of paragraph's cell
21
+ def to_s
22
+ paragraphs.map(&:text).join('')
23
+ end
24
+
25
+ # Array of paragraphs contained within cell
26
+ def paragraphs
27
+ @node.xpath('w:p').map {|p_node| Containers::Paragraph.new(p_node) }
28
+ end
29
+
30
+ # Iterate over each text run within a paragraph's cell
31
+ def each_paragraph
32
+ paragraphs.each { |tr| yield(tr) }
33
+ end
34
+
35
+ alias_method :text, :to_s
36
+ end
37
+ end
38
+ end
39
+ end
@@ -1,29 +1,29 @@
1
- require 'docx/containers/table_cell'
2
- require 'docx/containers/container'
3
-
4
- module Docx
5
- module Elements
6
- module Containers
7
- class TableColumn
8
- include Container
9
- include Elements::Element
10
-
11
- def self.tag
12
- 'w:gridCol'
13
- end
14
-
15
- def initialize(cell_nodes)
16
- @node = ''
17
- @properties_tag = ''
18
- @cells = cell_nodes.map { |c_node| Containers::TableCell.new(c_node) }
19
- end
20
-
21
- # Array of cells contained within row
22
- def cells
23
- @cells
24
- end
25
-
26
- end
27
- end
28
- end
29
- end
1
+ require 'docx/containers/table_cell'
2
+ require 'docx/containers/container'
3
+
4
+ module Docx
5
+ module Elements
6
+ module Containers
7
+ class TableColumn
8
+ include Container
9
+ include Elements::Element
10
+
11
+ def self.tag
12
+ 'w:gridCol'
13
+ end
14
+
15
+ def initialize(cell_nodes)
16
+ @node = ''
17
+ @properties_tag = ''
18
+ @cells = cell_nodes.map { |c_node| Containers::TableCell.new(c_node) }
19
+ end
20
+
21
+ # Array of cells contained within row
22
+ def cells
23
+ @cells
24
+ end
25
+
26
+ end
27
+ end
28
+ end
29
+ end
@@ -1,28 +1,28 @@
1
- require 'docx/containers/table_cell'
2
- require 'docx/containers/container'
3
-
4
- module Docx
5
- module Elements
6
- module Containers
7
- class TableRow
8
- include Container
9
- include Elements::Element
10
-
11
- def self.tag
12
- 'tr'
13
- end
14
-
15
- def initialize(node)
16
- @node = node
17
- @properties_tag = ''
18
- end
19
-
20
- # Array of cells contained within row
21
- def cells
22
- @node.xpath('w:tc').map {|c_node| Containers::TableCell.new(c_node) }
23
- end
24
-
25
- end
26
- end
27
- end
28
- end
1
+ require 'docx/containers/table_cell'
2
+ require 'docx/containers/container'
3
+
4
+ module Docx
5
+ module Elements
6
+ module Containers
7
+ class TableRow
8
+ include Container
9
+ include Elements::Element
10
+
11
+ def self.tag
12
+ 'tr'
13
+ end
14
+
15
+ def initialize(node)
16
+ @node = node
17
+ @properties_tag = ''
18
+ end
19
+
20
+ # Array of cells contained within row
21
+ def cells
22
+ @node.xpath('w:tc').map {|c_node| Containers::TableCell.new(c_node) }
23
+ end
24
+
25
+ end
26
+ end
27
+ end
28
+ end