|
1 | | -# docx |
2 | | - |
3 | | -a ruby library/gem for interacting with `.docx` files. currently capabilities include reading paragraphs/bookmarks, inserting text at bookmarks, and saving the document. |
4 | | - |
5 | | -## usage |
6 | | - |
7 | | -### install |
8 | | - |
9 | | -requires ruby (only tested with 1.9.3 so far) |
10 | | - |
11 | | - gem install docx |
12 | | - |
13 | | -### reading |
14 | | - |
15 | | -``` ruby |
16 | | -require 'docx' |
17 | | - |
18 | | -d = Docx::Document.open('example.docx') |
19 | | -# Array of paragraphs |
20 | | -d.paragraphs.each do |p| |
21 | | - puts d |
22 | | -end |
23 | | - |
24 | | -# Hash of Bookmarks. Bookmark names as keys correspond to bookmark objects. |
25 | | -d.bookmarks.each_pair do |bookmark_name, bookmark_object| |
26 | | - puts bookmark_name |
27 | | -end |
28 | | -``` |
29 | | - |
30 | | -### writing |
31 | | - |
32 | | -``` ruby |
33 | | -require 'docx' |
34 | | - |
35 | | -d = Docx::Document.open('example.docx') |
36 | | -# Insert a single line after a bookmark |
37 | | -d.bookmarks['example_bookmark'].insert_after("Hello world.") |
38 | | -# Each value in array is put on a separate line |
39 | | -d.bookmarks['example_bookmark'].insert_multiple_lines_after(['Hello', 'World', 'foo']) |
40 | | -``` |
41 | | - |
42 | | -### advanced |
43 | | - |
44 | | -``` ruby |
45 | | -require 'docx' |
46 | | - |
47 | | -d = Docx::Document.open('example.docx') |
48 | | - |
49 | | -# The Nokogiri node on which an element is based can be accessed using #node |
50 | | -d.paragraphs.each do |p| |
51 | | - puts p.node.inspect |
52 | | -end |
53 | | - |
54 | | -# The #xpath and #at_xpath are delegated to the node from the element, saving a step |
55 | | -p_element = d.paragraphs.first |
56 | | -p_children = p_element.xpath("//child::*") # selects all children |
57 | | -p_child = p_element.at_xpath("//child::*") # selects first child |
58 | | -``` |
59 | | - |
60 | | -## Development |
61 | | - |
62 | | -### todo |
63 | | - |
64 | | -* Add better formatting identification for specific nodes and other formatting indicators (text size, paragraph spacing) |
65 | | -* Calculate element formatting based on values present in element properties as well as properties inherited from parents |
66 | | -* Default formatting of inserted elements to inherited values |
67 | | -* Implement formattable elements. |
68 | | -* Implement styles. |
| 1 | +# docx |
| 2 | + |
| 3 | +a ruby library/gem for interacting with `.docx` files. currently capabilities include reading paragraphs/bookmarks, inserting text at bookmarks, and saving the document. |
| 4 | + |
| 5 | +## usage |
| 6 | + |
| 7 | +### install |
| 8 | + |
| 9 | +requires ruby (only tested with 1.9.3 so far) |
| 10 | + |
| 11 | + gem install docx |
| 12 | + |
| 13 | +### reading |
| 14 | + |
| 15 | +``` ruby |
| 16 | +require 'docx' |
| 17 | + |
| 18 | +# Create a Docx::Document object for our existing docx file |
| 19 | +doc = Docx::Document.open('example.docx') |
| 20 | + |
| 21 | +# Retrieve and display paragraphs |
| 22 | +doc.paragraphs.each do |p| |
| 23 | + puts p |
| 24 | +end |
| 25 | + |
| 26 | +# Retrieve and display bookmarks, returned as hash with bookmark names as keys and objects as values |
| 27 | +doc.bookmarks.each_pair do |bookmark_name, bookmark_object| |
| 28 | + puts bookmark_name |
| 29 | +end |
| 30 | +``` |
| 31 | + |
| 32 | +### writing |
| 33 | + |
| 34 | +``` ruby |
| 35 | +require 'docx' |
| 36 | + |
| 37 | +# Create a Docx::Document object for our existing docx file |
| 38 | +doc = Docx::Document.open('example.docx') |
| 39 | + |
| 40 | +# Insert a single line of text after one of our bookmarks |
| 41 | +doc.bookmarks['example_bookmark'].insert_after("Hello world.") |
| 42 | + |
| 43 | +# Insert multiple lines of text at our bookmark |
| 44 | +doc.bookmarks['example_bookmark_2'].insert_multiple_lines_after(['Hello', 'World', 'foo']) |
| 45 | + |
| 46 | +# Save document to specified path |
| 47 | +doc.save('example-edited.docx') |
| 48 | +``` |
| 49 | + |
| 50 | +### advanced |
| 51 | + |
| 52 | +``` ruby |
| 53 | +require 'docx' |
| 54 | + |
| 55 | +d = Docx::Document.open('example.docx') |
| 56 | + |
| 57 | +# The Nokogiri::XML::Node on which an element is based can be accessed using #node |
| 58 | +d.paragraphs.each do |p| |
| 59 | + puts p.node.inspect |
| 60 | +end |
| 61 | + |
| 62 | +# The #xpath and #at_xpath methods are delegated to the node from the element, saving a step |
| 63 | +p_element = d.paragraphs.first |
| 64 | +p_children = p_element.xpath("//child::*") # selects all children |
| 65 | +p_child = p_element.at_xpath("//child::*") # selects first child |
| 66 | +``` |
| 67 | + |
| 68 | +## Development |
| 69 | + |
| 70 | +### todo |
| 71 | + |
| 72 | +* Add better formatting identification for specific nodes and other formatting indicators (text size, paragraph spacing) |
| 73 | +* Calculate element formatting based on values present in element properties as well as properties inherited from parents |
| 74 | +* Default formatting of inserted elements to inherited values |
| 75 | +* Implement formattable elements. |
| 76 | +* Implement styles. |
69 | 77 | * Easier multi-line text insertion at a single bookmark (inserting paragraph nodes after the one containing the bookmark) |
0 commit comments