Skip to content

Getting Started ar

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

البدء مع docxi: أول مستند Word لك باستخدام Ruby

يأخذك هذا الدليل من مجلد فارغ إلى ملف .docx منسّق. يفترض الدليل أنك تعرف أساسيات Ruby، ولا تحتاج إلى Rails أو Microsoft Word.

1. ثبّت الحزمة

gem install docxi

في مشروع يحتوي على Gemfile، أضف gem "docxi", "~> 0.1" وشغّل bundle install بدلًا من ذلك. تجد التفاصيل في صفحة التثبيت.

2. أنشئ مستندًا

احفظ ما يلي في ملف باسم 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)

شغّل ruby hello.rb (أو bundle exec ruby hello.rb في مشروع يستخدم Bundler) ثم افتح hello.docx.

3. ما الذي يفعله كل سطر

  • ينشئ Docxi::Document.new مستندًا فارغًا بهوامش الصفحة الافتراضية.
  • يمثّل word.document متن المستند، وكل ما تضيفه يمر عبره.
  • يضيف document.p فقرة. الخيارات التي تمررها إلى p، مثل align:، تنطبق على الفقرة وعلى النص الذي بداخلها.
  • يضيف p.text مقطعًا نصيًا (run). وحدة size: هي النقطة.
  • يبني word.render حزمة .docx ويعيد كائن StringIO، وتحتوي قيمته .string على بايتات الملف.

4. أضف قائمة وجدولًا

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)

أمران يجب الانتباه إليهما:

  • يحافظ الخيار space: true على المسافة في بداية المقطع أو نهايته. من دونه يحذف Word المسافة فتلتصق الكلمات ببعضها.
  • الخيارات المحددة على tr تنطبق على كل خلية في ذلك الصف، ولذلك يظهر صف العناوين كله مظللًا وبخط غامق.

5. الخطوات التالية


→ الصفحة الرئيسية للويكي · README

Clone this wiki locally