Skip to content

Getting Started de

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

Erste Schritte mit docxi: Ihr erstes Word-Dokument in Ruby

Diese Anleitung führt Sie von einem leeren Ordner zu einer formatierten .docx-Datei. Sie setzt Ruby-Grundkenntnisse voraus. Rails oder Microsoft Word brauchen Sie nicht.

1. Gem installieren

gem install docxi

In einem Projekt mit Gemfile fügen Sie stattdessen gem "docxi", "~> 0.1" hinzu und führen bundle install aus. Die Details stehen unter Installation.

2. Dokument erstellen

Speichern Sie Folgendes als 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)

Führen Sie ruby hello.rb aus (oder bundle exec ruby hello.rb in einem Bundler-Projekt) und öffnen Sie hello.docx.

3. Was jede Zeile macht

  • Docxi::Document.new erstellt ein leeres Dokument mit den Standard-Seitenrändern.
  • word.document ist der Hauptteil des Dokuments. Alles, was Sie hinzufügen, läuft darüber.
  • document.p fügt einen Absatz hinzu. Optionen für p, etwa align:, gelten für den Absatz und den Text darin.
  • p.text fügt einen Textlauf (Run) hinzu. size: wird in Punkt angegeben.
  • word.render baut das .docx-Paket und gibt ein StringIO zurück. Dessen .string enthält die Bytes der Datei.

4. Liste und Tabelle hinzufügen

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)

Zwei Dinge sind wichtig:

  • space: true erhält das Leerzeichen am Anfang oder Ende eines Textlaufs. Ohne diese Option entfernt Word das Leerzeichen, und die Wörter laufen ineinander.
  • Optionen an tr gelten für jede Zelle der Zeile. Deshalb ist die ganze Kopfzeile der Tabelle schattiert und fett.

5. Wie es weitergeht

  • Anwendungsbeispiele: vollständige Rezepte für Berichte, Rechnungen und Briefe
  • Funktionen: alle Optionen für Absätze, Text, Tabellen, Bilder, Kopf- und Fußzeilen
  • Integrationen: das Dokument aus einem Rails-Controller senden
  • Best Practices: typische Überraschungen vermeiden, bevor sie auftreten

← Wiki-Startseite · README

Clone this wiki locally