Repository navigation
Getting Started
Hammad Maqbool edited this page Oct 29, 2026
·
1 revision
English · 简体中文 · 繁體中文 · Español · العربية · Français · Português (Brasil) · Deutsch · हिन्दी · 日本語 · 한국어 · Русский · Bahasa Indonesia · Türkçe · Italiano · Tiếng Việt
This guide takes you from an empty folder to a formatted .docx file. It assumes you know basic Ruby. You don't need Rails or Microsoft Word.
gem install docxiIn a project with a Gemfile, add gem "docxi", "~> 0.1" and run bundle install instead. Installation has the details.
Save this as hello.rb:
require "docxi"
word = Docxi::Document.new
document = word.document
document.p(align: "center") do |p|
p.text "Hello, Word", size: 24, bold: true
end
File.binwrite("hello.docx", word.render.string)Run ruby hello.rb (or bundle exec ruby hello.rb in a Bundler project) and open hello.docx.
-
Docxi::Document.newcreates an empty document with the default page margins. -
word.documentis the body of the document. Everything you add goes through it. -
document.padds a paragraph. Options given top, such asalign:, apply to the paragraph and to the text inside it. -
p.textadds a run of text.size:is in points. -
word.renderbuilds the.docxpackage and returns aStringIO. Its.stringholds the bytes of the file.
require "docxi"
word = Docxi::Document.new
document = word.document
document.p { |p| p.text "Quarterly summary", size: 16, bold: true }
document.p do |p|
p.text "Revenue grew ", space: true
p.text "12%", bold: true, color: "007A33"
p.text " compared with last quarter.", space: true
end
document.p(ul: true) { |p| p.text "New customers: 48" }
document.p(ul: true) { |p| p.text "Customers lost: 3" }
document.table(columns_width: [200, 100]) do |table|
table.tr(fill: "D9D9D9", bold: true) do |tr|
tr.tc { |tc| tc.text "Region" }
tr.tc { |tc| tc.text "Sales" }
end
table.tr do |tr|
tr.tc { |tc| tc.text "North" }
tr.tc { |tc| tc.text "$41,000", align: "right" }
end
end
File.binwrite("summary.docx", word.render.string)Two things to notice:
-
space: truekeeps the space at the start or end of a run. Without it, Word drops the space and the words run together. - Options on
trapply to every cell in that row, so the whole header row is shaded and bold.
- Usage Examples: complete recipes for reports, invoices and letters
- Features: every option for paragraphs, text, tables, images, headers and footers
- Integrations: sending the document from a Rails controller
- Best Practices: avoid the common surprises before they happen
docxi wiki · README · Installation · Troubleshooting · Report an issue · MIT License
Start
Build documents
Help
Project
Links
Languages