Open Source Ruby Library for Word Documents Processing 

Free Ruby API that enables software developers to generate and Edit Microsoft Word files, manage headers & footers, insert and edit tables, and much more.

Docx is an open source JavaScript API that provides the capability to generate and manage Word Docx files inside their own JavaScript application with ease. The library can smoothly work for Node as well as on the Browser. The library is very stable and easy to use. With just a couple of lines of code, developers can create and manipulate word documents without any external dependencies.

The DocX API has included support for several important features related to working with Word documents such as creating Word documents, modifying DOCX files, adding a paragraph to a word file, adding and managing headers & footers, inserting and editing tables, bullet and numbering support, Table of content creation, set document margins, set page size, text alignment, Manage fonts and font sizes, document sections creation and many more.

Previous Next

Getting Started with Docx

The recommended way to install is using npm. Please use the following command to your application's Gemfile

Install Docs via npm

 gem install docx

Write to Existing DOCX Files via Ruby

The Ruby Docx library enables software developers to open existing DOCX files and update the content of a file inside their own Ruby applications. To open a file you need to provide the correct path of the existing DOCX file. Once you can have access to the documents you can easily add a single line of text or paragraphs, substitute text, remove unwanted content, modify the existing text, and so on. Once everything is properly completed, you can save the document to the specified path.

How to Write to Existing DOCX Files via Ruby API

require 'docx'
doc = Docx::Document.open('example.docx')
doc.bookmarks['example_bookmark'].insert_text_after("Hello world.")

# Insert multiple lines of text at our bookmark
doc.bookmarks['example_bookmark_2'].insert_multiple_lines_after(['Hello', 'World', 'foo'])

# Remove paragraphs
doc.paragraphs.each do |p|
  p.remove! if p.to_s =~ /TODO/
end

# Substitute text, preserving formatting
doc.paragraphs.each do |p|
  p.each_text_run do |tr|
    tr.substitute('_placeholder_', 'replacement value')
  end
end

# Save document to specified path
doc.save('example-edited.docx')

Reading Docx File via Ruby Library

The open source Ruby Docx library has provided functionality for accessing and reading MS word DOCX files using a couple of lines of Ruby code. Developers can easily create the document object for our existing Docx file and can retrieve and display the content of the file with just a couple of lines of Ruby code. You easily display a particular paragraph or bookmark. You can also display files from a buffer.

Open & Read Existing Docx File via Ruby API

require 'docx'

# Create a Docx::Document object for our existing docx file
doc = Docx::Document.open('example.docx')

# Retrieve and display paragraphs
doc.paragraphs.each do |p|
  puts p
end

# Retrieve and display bookmarks, returned as hash with bookmark names as keys and objects as values
doc.bookmarks.each_pair do |bookmark_name, bookmark_object|
  puts bookmark_name
end

Reading Tables in Word DOCX Files

The open source Ruby DOCX library gives software developers the capability to access and read tables inside a DOCX file using Ruby commands. You can easily access rows, columns, and cells of tables with just a couple of lines of code. The library supports iterating through tables, Row-based iteration, and Column-based iteration.

How to Read Tables in Word Documents via Ruby API

require 'docx'

# Create a Docx::Document object for our existing docx file
doc = Docx::Document.open('tables.docx')

first_table = doc.tables[0]
puts first_table.row_count
puts first_table.column_count
puts first_table.rows[0].cells[0].text
puts first_table.columns[0].cells[0].text

# Iterate through tables
doc.tables.each do |table|
  table.rows.each do |row| # Row-based iteration
    row.cells.each do |cell|
      puts cell.text
    end
  end

  table.columns.each do |column| # Column-based iteration
    column.cells.each do |cell|
      puts cell.text
    end
  end
end
 English