docx-cloner 0.1.0 → 0.1.1
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 +8 -8
- data/README.md +16 -2
- data/docx-examples/test.rb +15 -0
- data/lib/docx/cloner.rb +6 -55
- data/lib/docx/cloner/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
YTM4MjRmN2JhOTQ2YzNlZGY2NzJiYjM4ZWRiOGJjODk5ZTExYzE1MA==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
MGVhNzYxYmNjZTQxYTYyNzhhNzVkMDgyOTcxNDVjMjI1NWY3NTZmMA==
|
7
7
|
!binary "U0hBNTEy":
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
NTNmNDI5NWJhYTc2YjAzZDU2Njc1ZDQ0MDdjNjg4ODNjYTIyYzI2YmIxYjZi
|
10
|
+
ZjM0YTBhYjI0NjQyYzhhMGM2OTEyMWVlYmFjMGU2NDczNGI4MGVlY2Q5OTdj
|
11
|
+
OGY2MjE0OTZmMjlmNTZmZDc2YmEwMmY4MWFkYTY3NWNiODkxODU=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
YWI4M2NiNDBlNDc1NGI2MmY1YzNkYmNlZTkyNzU4MTI2MjkxNDg2ZTI2OTQy
|
14
|
+
N2JmZTA1YjQzNmJiOTE3MGU1ZDExMWJkZjNkOWQ3YWI5MzIyMTJlOGYxYjg0
|
15
|
+
MTVjNTc3NDVlNzM4Njc0NDNiNTI4NTgxZWI2OGM3MDc0ZWYzOTE=
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Docx::Cloner
|
2
2
|
|
3
|
-
|
3
|
+
This is a tool to clone docx file with tag field. And it supported utf-8 well.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -17,8 +17,22 @@ Or install it yourself as:
|
|
17
17
|
$ gem install docx-cloner
|
18
18
|
|
19
19
|
## Usage
|
20
|
+
#encoding: utf-8
|
21
|
+
require 'docx/cloner'
|
22
|
+
|
23
|
+
sourc_file = 'source.docx'
|
24
|
+
dest_file = 'dest.docx'
|
25
|
+
docx = Docx::Cloner::DocxTool.new sourc_file
|
26
|
+
|
27
|
+
docx.set_single_tag '{Name}', '周大福'
|
28
|
+
|
29
|
+
table_title = ["{名称1}", "{00.01}"]
|
30
|
+
table_data = [["自行车1", "125.00"], ["大卡车1", "256500.00"]]
|
31
|
+
docx.set_row_tags table_title, table_data, 'tr'
|
32
|
+
|
33
|
+
docx.save dest_file
|
34
|
+
docx.release
|
20
35
|
|
21
|
-
TODO: Write usage instructions here
|
22
36
|
|
23
37
|
## Contributing
|
24
38
|
|
@@ -0,0 +1,15 @@
|
|
1
|
+
#encoding: utf-8
|
2
|
+
require 'docx/cloner'
|
3
|
+
|
4
|
+
sourc_file = 'source.docx'
|
5
|
+
dest_file = 'dest.docx'
|
6
|
+
docx = Docx::Cloner::DocxTool.new sourc_file
|
7
|
+
|
8
|
+
docx.set_single_tag '{Name}', '周大福'
|
9
|
+
|
10
|
+
table_title = ["{名称1}", "{00.01}"]
|
11
|
+
table_data = [["自行车1", "125.00"], ["大卡车1", "256500.00"]]
|
12
|
+
docx.set_row_tags table_title, table_data, 'tr'
|
13
|
+
|
14
|
+
docx.save dest_file
|
15
|
+
docx.release
|
data/lib/docx/cloner.rb
CHANGED
@@ -5,55 +5,6 @@ require 'nokogiri'
|
|
5
5
|
|
6
6
|
module Docx
|
7
7
|
module Cloner
|
8
|
-
class WordXmlFile
|
9
|
-
def self.open(path, &block)
|
10
|
-
self.new(path, &block)
|
11
|
-
end
|
12
|
-
|
13
|
-
def initialize(path, &block)
|
14
|
-
@replace = {}
|
15
|
-
if block_given?
|
16
|
-
@zip = Zip::ZipFile.open(path)
|
17
|
-
yield self
|
18
|
-
@zip.close
|
19
|
-
else
|
20
|
-
@zip = Zip::ZipFile.open(path)
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
def merge(rec)
|
25
|
-
_xml = @zip.read("word/document.xml")
|
26
|
-
doc = Nokogiri::XML(_xml)
|
27
|
-
tags = doc.root.xpath("//w:t[contains(., '_Name')]")
|
28
|
-
tags.each do |field|
|
29
|
-
new_field = field
|
30
|
-
if field.content == 'First_Name'
|
31
|
-
field.inner_html = 'Adi'
|
32
|
-
new_field.inner_html = 'My Adi'
|
33
|
-
field.add_next_sibling(new_field.to_html)
|
34
|
-
elsif field.content == 'Last_Name'
|
35
|
-
field.inner_html = 'Zhou'
|
36
|
-
end
|
37
|
-
end
|
38
|
-
@replace["word/document.xml"] = doc.serialize :save_with => 0
|
39
|
-
end
|
40
|
-
|
41
|
-
def save(path)
|
42
|
-
Zip::ZipFile.open(path, Zip::ZipFile::CREATE) do |out|
|
43
|
-
@zip.each do |entry|
|
44
|
-
out.get_output_stream(entry.name) do |o|
|
45
|
-
if @replace[entry.name]
|
46
|
-
o.write(@replace[entry.name])
|
47
|
-
else
|
48
|
-
o.write(@zip.read(entry.name))
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|
53
|
-
@zip.close
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
8
|
class DocxTool
|
58
9
|
|
59
10
|
'加载docx文件,将段落存储到@paragraph,用@paragraph[:text_content]检索,再从段落内检索xml标签位置'
|
@@ -148,7 +99,7 @@ module Docx
|
|
148
99
|
|
149
100
|
def generate_paragraph node
|
150
101
|
paragraphs = []
|
151
|
-
puts "查找范围:#{node.path}"
|
102
|
+
#puts "查找范围:#{node.path}"
|
152
103
|
wp_set = node.xpath(".//w:p")
|
153
104
|
#puts "#{wp_set.size}'s wp"
|
154
105
|
wp_set.each do |wp|
|
@@ -202,7 +153,7 @@ module Docx
|
|
202
153
|
end
|
203
154
|
|
204
155
|
if dest.size > 0
|
205
|
-
puts "被替换节点:#{dest.first.path}"
|
156
|
+
#puts "被替换节点:#{dest.first.path}"
|
206
157
|
dest.first.content = value
|
207
158
|
dest[1..-1].each do |node|
|
208
159
|
#puts node
|
@@ -225,24 +176,24 @@ module Docx
|
|
225
176
|
def clone_tag_scope node, times
|
226
177
|
#puts "clone #{node.node_name} #{times} times"
|
227
178
|
nodes = Array.new times
|
228
|
-
puts "被克隆节点:#{node.path}"
|
179
|
+
#puts "被克隆节点:#{node.path}"
|
229
180
|
times.downto(1).each do |_i|
|
230
181
|
i = _i.to_i - 1
|
231
182
|
nodes[i] = node.dup
|
232
183
|
node.add_next_sibling nodes[i]
|
233
|
-
puts "第#{i+1}个节点克隆:#{nodes[i].path}"
|
184
|
+
#puts "第#{i+1}个节点克隆:#{nodes[i].path}"
|
234
185
|
end
|
235
186
|
return nodes
|
236
187
|
end
|
237
188
|
|
238
189
|
#根据行标签设置,替换成多行数据,这里考虑表格的一般情况
|
239
190
|
def set_row_tags tags, values, type
|
240
|
-
puts "tags:#{tags}, values:#{values}, type:#{type}"
|
191
|
+
#puts "tags:#{tags}, values:#{values}, type:#{type}"
|
241
192
|
#找到标签所在行的父节点
|
242
193
|
tag_scope_node = get_tag_scope tags.first, type
|
243
194
|
value_scope_nodes = clone_tag_scope tag_scope_node, values.size
|
244
195
|
value_scope_nodes.each_with_index do |node, r|
|
245
|
-
puts "查找范围:#{node.path}"
|
196
|
+
#puts "查找范围:#{node.path}"
|
246
197
|
tags.each_with_index do |tag, c|
|
247
198
|
replace_tag tag, values[r][c], node
|
248
199
|
end
|
data/lib/docx/cloner/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: docx-cloner
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- homeway
|
@@ -55,6 +55,7 @@ files:
|
|
55
55
|
- docx-examples/read-single-tags-body.xml
|
56
56
|
- docx-examples/read-single-tags.docx
|
57
57
|
- docx-examples/source.docx
|
58
|
+
- docx-examples/test.rb
|
58
59
|
- docx-examples/wp.xml
|
59
60
|
- features/read.feature
|
60
61
|
- features/replace.feature
|