1. 产品
  2.   字处理
  3.   Ruby
  4.   HTMLtoWord
 
  

用于从 HTML 创建 Word 文档的免费 Ruby 库

开源 Ruby API,使软件开发者能够从简单的 HTML 文件创建 MS Word DOCX 文档。轻松生成动态报告和表单。

HTMLtoWord 是什么?

在快速发展的软件开发世界中,效率和简洁性至关重要。文档生成是一项常见任务,而在将 HTML 内容转换为 Microsoft Word 文档时,开发者往往寻找可靠且简便的解决方案。这时 Ruby HTML 库 HTMLtoWord 崭露头角。该库拥有多项重要功能,例如快速将 HTML 文档转换为 Word、支持自定义样式和格式、处理嵌入 HTML 内容的图像、渲染 CSS 样式、支持文本高亮、创建分页符等。

HTMLtoWord 是一个 Ruby 库,旨在轻松将 HTML 文档转换为 Microsoft Word(.docx)格式。利用该库的强大功能,软件专业人员可以简化从 HTML 内容生成丰富格式的 Word 文档的过程,从而节省时间和精力。该库拥有直观且用户友好的 API,便于集成到已有的 Ruby 项目中。开发者只需最少的工作量即可开始使用,节省宝贵的时间和资源。

Previous Next

开始使用 HTMLtoWord

HTMLtoWord 需要 Ruby 2.5 或更高版本。

通过 RubyGems 安装文档

 gem install openxml-docx
You can also download it directly from GitHub.

通过 Ruby 将 HTML 转换为 Word DOCX

推荐的安装 HTMLtoWord 方法是使用 RubyGems。请使用以下命令进行顺畅安装。

如何通过 Ruby API 将 HTML 文件转换为 Word Docx 文档?

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')

自定义样式和格式支持

开源 HTMLtoWord 库已完整支持在 Ruby 应用中将 HTML 内容转换为 Microsoft Word 文档。它是一个可靠且简洁的解决方案,提供单一方法 Htmltoword::Document.create,接受 HTML 字符串作为输入并返回相应的 Word 文档。下面的示例展示了软件开发者如何在 Ruby 应用中从 HTML 文件创建 Word DOCX 文件。

如何在 Ruby 应用中为表格应用样式?

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')

通过 Ruby 处理 HTML 导出到 DOCX 时的图像处理

Hello, World!

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

使用 HTMLtoWord 的最大优势之一是它能够保留 HTML 源的丰富样式和格式。无论是粗体文本、斜体、表格、图像或其他元素,库都能确保最终的 Word 文档忠实呈现原始 HTML 内容。库允许软件开发者通过在 HTML 内容中使用 CSS 属性来控制生成的 Word 文档的外观。下面的示例展示了如何在 Ruby 应用中对现有表格应用样式。

 中国人