Open Source Ruby Library to Create Word Documents & Add Tables
Free Ruby Gem (Library) That enables the Creation of Microsoft Word .docx Files using the Office Open XML (OOXML) Format and Add Text Paragraphs, Tables and Images inside DOCX files.
What is OpenXml::Docx?
Microsoft Word documents have become the standard for business communications, reports, and documentation across industries. While manual document creation works for small tasks, automating document generation becomes essential when dealing with templates, bulk operations, or dynamic content. This is where OpenXml::Docx comes into play—a Ruby library that empowers software developers to create professional Word documents programmatically. The library runs anywhere Ruby runs, with no external dependencies. There are several important features part of the library, such as creating new word documents, applying text formatting and styling, tables and data presentation, images and media integration and so on.
OpenXml::Docx is a Ruby gem that provides a clean and intuitive interface for generating Word documents in the Open XML format (the .docx extension we all know). Unlike heavyweight solutions that require Microsoft Office installation, this library works independently, creating documents from scratch using pure Ruby code. It follows the Office Open XML standard, ensuring compatibility with Microsoft Word, Google Docs, LibreOffice, and other modern word processors. The library focuses on simplicity and readability, making document generation accessible even to developers who aren't familiar with the complex underlying XML structure of Word documents. Instead of wrestling with XML namespaces and schemas, you can write straightforward Ruby code that feels natural and maintainable.
Getting Started with OpenXml::Docx
The recommend way to install OpenXml::Docx is using RubyGems. Please use the following command for a smooth installation.
Install OpenXml::Docx via RubyGems
gem install openxml-docxYou can also download it directly from GitHub.Creating Your First DOCX Document
The open source OpenXml::Docx has included complete support for creating and managing new Word documents inside Java applications. The fundamental building block of a Word document is the paragraph. With just a couple of lines of code, software developers can add text and images inside Word documents. Here is a simple example that demonstrates how software developers can access the document body, add a paragraph with a text run, apply formatting, and save the file.
How to Create a Word Document and Add Paragraph with Text via Java API?
require 'openxml/docx'
# Create a new document package
package = OpenXml::Docx::Package.new
# Access the main document
doc = package.document
# Add a paragraph with text
paragraph = doc.add_paragraph
run = paragraph.add_run
run.text = "Welcome to OpenXml::Docx"
run.bold = true
run.font_size = 24
# Save the document
package.save('welcome.docx')
Advanced Text Formatting via Java
The open source OpenXml::Docx library has included complete support for basic as well as advanced text formatting inside Word documents. Beyond basic bold and italic, the library supports sophisticated text formatting. The following Java code example showcases underlines, strikethroughs, highlighting, and combinations of multiple effects. The color property accepts hex values, allowing precise color matching to brand guidelines.
How to Apply Text Formatting to Word Documents via Java API?
require 'openxml/docx'
package = OpenXml::Docx::Package.new
doc = package.document
# Demonstrate various text effects
para1 = doc.add_paragraph
run1 = para1.add_run
run1.text = "This text is underlined"
run1.underline = :single
para2 = doc.add_paragraph
run2 = para2.add_run
run2.text = "This text has a strikethrough"
run2.strike = true
para3 = doc.add_paragraph
run3 = para3.add_run
run3.text = "This text is highlighted"
run3.highlight = "yellow"
para4 = doc.add_paragraph
run4 = para4.add_run
run4.text = "This combines multiple effects"
run4.bold = true
run4.italic = true
run4.color = "FF0000"
run4.font_size = 16
package.save('formatted_text.docx')
Creating Tables for Data in Word Files via Java
Tables are essential for presenting structured information. The OpenXml::Docx library makes it easy for software developers to create tables inside Word documents to manage data in proper way inside Java applications. Here's a practical that can creates a professional sales table with a header row in bold and three data rows. The code demonstrates how to access specific cells and populate them with content. You can iterate through data structures and dynamically populate tables, making this perfect for report generation.
How to Create a Table with Rows and Columns inside Java Apps?
require 'openxml/docx'
package = OpenXml::Docx::Package.new
doc = package.document
# Add a heading
heading = doc.add_paragraph
heading_run = heading.add_run
heading_run.text = "Sales Summary"
heading_run.bold = true
heading_run.font_size = 18
# Create a table with 3 columns and 4 rows
table = doc.add_table(rows: 4, cols: 3)
# Header row
table.rows[0].cells[0].add_paragraph.add_run.text = "Product"
table.rows[0].cells[1].add_paragraph.add_run.text = "Units Sold"
table.rows[0].cells[2].add_paragraph.add_run.text = "Revenue"
# Make header row bold
table.rows[0].cells.each do |cell|
cell.paragraphs[0].runs[0].bold = true
end
# Data rows
products = [
["Widget A", "1,250", "$25,000"],
["Widget B", "890", "$17,800"],
["Widget C", "2,100", "$42,000"]
]
products.each_with_index do |product, index|
row = table.rows[index + 1]
product.each_with_index do |value, col_index|
row.cells[col_index].add_paragraph.add_run.text = value
end
end
package.save('sales_summary.docx')
Automated Report & Invoice Generation
Companies can use OpenXml::Docx library to generate monthly reports, financial statements, or analytics summaries automatically. By pulling data from databases and formatting it into professional documents, organizations save countless hours of manual work. Moreover, E-commerce platforms and service businesses can generate invoices and contracts dynamically. Customer information, line items, and terms can be populated from databases, ensuring accuracy and consistency across all documents.