使用IronWord從DOCX中提取文字
IronWord的ExtractText()方法使您能夠從DOCX文件中提取文字,方式包括存取整個文件、特定段落或表格單元格,為使用C#進行文件處理和資料分析任務提供簡單的API。
-
1Install IronWord with NuGet Package Manager
-
2複製並運行這段程式碼片段。
using IronWord; // Quick example: Extract all text from DOCX WordDocument doc = new WordDocument("sample.docx"); string allText = doc.ExtractText(); Console.WriteLine(allText);C# -
3部署以在您的實時環境中測試
今天就開始在您的專案中使用IronWord,透過免費試用
最少工作流程(5步驟)
- Install the IronWord C# library
- 使用
new WordDocument()載入現有的Word文件 - 在文件上調用
ExtractText()以檢索所有文字 - 使用
Paragraphs集從特定段落中提取文字 - 處理或導出提取出的文字內容
我如何從DOCX文件中提取所有文字?
ExtractText()方法從整個Word文件中檢索文字內容。 在此範例中,我們建立了一個新文件,向其中新增文字,使用ExtractText()提取文字,並在控制台中顯示。 這演示了主要的文字提取工作流程。
提取出的文字保持文件的邏輯閱讀順序。 該方法按順序處理標題、段落、列表和其他文字元素,這使其特別適合於內容分析和搜索索引應用。
using System;
using IronWord;
// Instantiate a new DOCX file
WordDocument doc = new WordDocument();
// Add text
doc.AddText("Hello, World!");
// Print extracted text from the document to the console
Console.WriteLine(doc.ExtractText());Imports System
Imports IronWord
' Instantiate a new DOCX file
Dim doc As New WordDocument()
' Add text
doc.AddText("Hello, World!")
' Print extracted text from the document to the console
Console.WriteLine(doc.ExtractText())提取出的文字看起來像什麼?

我應該在控制台中期望什麼輸出?

我如何從特定段落中提取文字?
為了更好的控制,您可以從特定段落中提取文字而不是整個文件。 通過存取Paragraphs集合,您可以定位和處理所需的段落。 這種細粒度的方法在處理具有結構化內容的文件或需要單獨處理特定部分時非常有用。
在此範例中,我們從第一個和最後一個段落中提取文字,將其結合,並將結果保存到.txt文件中。這種技術在文件摘要工具中常用,您可能希望提取文件的引言和結論。 類似於如何使用授權金鑰解鎖功能,Paragraphs集合讓您可以存取特定的文件元素。
using System.IO;
using System.Linq;
using IronWord;
// Load an existing DOCX file
WordDocument doc = new WordDocument("document.docx");
// Extract text and assign variables
string firstParagraph = doc.Paragraphs[0].ExtractText();
string lastParagraph = doc.Paragraphs.Last().ExtractText();
// Combine the texts
string newText = firstParagraph + " " + lastParagraph;
// Export the combined text as a new .txt file
File.WriteAllText("output.txt", newText);Imports System.IO
Imports System.Linq
Imports IronWord
' Load an existing DOCX file
Dim doc As New WordDocument("document.docx")
' Extract text and assign variables
Dim firstParagraph As String = doc.Paragraphs(0).ExtractText()
Dim lastParagraph As String = doc.Paragraphs.Last().ExtractText()
' Combine the texts
Dim newText As String = firstParagraph & " " & lastParagraph
' Export the combined text as a new .txt file
File.WriteAllText("output.txt", newText)結合文件分析需求,提取特定段落的能力顯得格外強大。 例如,您可能根據其格式、位置或內容模式提取關鍵段落。 這種選擇性提取方法有助於縮短處理時間並集中於最相關的內容。
從第一段提取的內容是什麼?

從最後一段提取的內容是什麼?

結合的文字在輸出文件中怎麼顯示?

上面的截圖展示了第一段提取、最後一段提取以及保存到文字文件的結合輸出。請注意提取過程如何在移除格式化資訊的同時保留文字內容,使其適合純文字處理。
我如何從DOCX中的表格中提取資料?
表格通常包含需要提取以進行處理或分析的結構化資料。 IronWord允許您通過遍歷行和單元格存取表格資料。 在此範例中,我們載入了一個包含API統計表的文件,並從第二行的第四列中提取特定的單元格值。
表格提取對資料遷移項目、報告生成和自動資料收集工作流程至關重要。 處理表格資料時,理解零基索引系統很重要——第一個表格是Rows[0],依此類推。 這種系統化的方法,類似於授權結構,提供了可預測的存取模式。
using System;
using IronWord;
using IronWord.Models;
// Load the API statistics document
WordDocument apiStatsDoc = new WordDocument("api-statistics.docx");
// Extract text from the 1st table, 4th column and 3rd row
string extractedValue = ((TableCell)apiStatsDoc.Tables[0].Rows[2].Cells[3]).ExtractText();
// Print extracted value
Console.WriteLine($"Target success rate: {extractedValue}");
程式碼演示了如何使用集合屬性Tables, Rows, 和 Cells存取表格單元格。 請注意,Cells 集合返回((TableCell)cell).ExtractText()。 這需要將using IronWord.Models;新增到您的命名空間聲明中。
源表格看起來是什麼樣的?

從表格單元格中檢索到的值是什麼?

高級文字提取場景
處理複雜的文件時,您可能需要結合多種提取技術。 這是一個範例,演示了從多個元素中提取文字並以不同方式處理它們:
using IronWord;
using IronWord.Models;
using System.Text;
using System.Linq;
// Load a complex document
WordDocument complexDoc = new WordDocument("report.docx");
// Create a StringBuilder for efficient string concatenation
StringBuilder extractedContent = new StringBuilder();
// Extract and process headers (assuming they're in the first few paragraphs)
var headers = complexDoc.Paragraphs
.Take(3)
.Select(p => p.ExtractText())
.Where(text => !string.IsNullOrWhiteSpace(text));
foreach (var header in headers)
{
extractedContent.AppendLine($"HEADER: {header}");
}
// Extract table summaries
foreach (var table in complexDoc.Tables)
{
// Get first cell as table header/identifier
string tableIdentifier = ((TableCell)table.Rows[0].Cells[0]).ExtractText();
extractedContent.AppendLine($"\nTABLE: {tableIdentifier}");
// Extract key metrics (last row often contains totals)
if (table.Rows.Count > 1)
{
var lastRow = table.Rows.Last();
var totals = lastRow.Cells.Select(cell => ((TableCell)cell).ExtractText());
extractedContent.AppendLine($"Totals: {string.Join(", ", totals)}");
}
}
// Save the structured extraction
System.IO.File.WriteAllText("structured-extract.txt", extractedContent.ToString());
此高級範例展示了如何通過組合不同的文件元素來建立結構化提取。 這種方法對於生成文件摘要、建立索引或為進一步處理準備資料特別有用。 就如同升級增強了軟體功能,結合提取方法增強了您的文件處理能力。
文字提取最佳實踐
在生產應用中實施文字提取時,請考慮以下最佳實踐:
-
錯誤處理:總是將提取程式碼包圍在try-catch塊中以處理可能損壞或結構異常的文件。
-
性能優化:對於大型文件或批處理,請考慮僅提取所需部分而非整個文件內容。
-
字元編碼:保存提取的文字時請注意字元編碼,特別是對於包含特殊字元或多語言的文件。
-
記憶體管理:處理多個文件時,正確處置
WordDocument物件以防止記憶體泄漏。
記住,文字提取保留了邏輯閱讀順序但移除了格式。 如果需要保持格式資訊,請考慮使用額外的IronWord功能或分開儲存元資料。 對於生產部署,請檢查變更日誌以保持更新最新功能和改進。
摘要
IronWord的ExtractText()方法提供了一種強大而靈活的方法來從DOCX文件中提取文字。 無論您需要提取整個文件、特定段落或表格資料,該API都提供了完成目標的直接方法。 通過將這些技術與適當的錯誤處理和優化策略相結合,您可以構建健壯的文件處理應用,能有效地處理各種文字提取場景。
對於更高級的場景以及探索附加功能,請查看擴展和其他文件資源,以增強您的文件處理能力。
常見問題
如何在C#中從Word文件提取所有文字?
在WordDocument物件上使用IronWord的ExtractText()方法。只需使用WordDocument doc = new WordDocument("document.docx");載入您的DOCX文件,然後呼叫string text = doc.ExtractText();來從文件中檢索所有文字內容。
我可以從特定段落提取文字而不是整個文件嗎?
可以,IronWord允許您透過存取段落集合來從特定段落提取文字。使用doc.Paragraphs[index].ExtractText()來目標個別段落以進行更細緻的文字提取。
如何從DOCX文件中的表格中提取文字?
IronWord透過Tables集合來啟用表格文字提取。使用doc.Tables[0].Rows[0].Cells[0].ExtractText()存取特定單元格,可以從您文件中任何表格單元格檢索文字內容。
使用ExtractText()提取的文字遵循什麼順序?
IronWord的ExtractText()方法保持文件的邏輯閱讀順序,按序處理標題、段落、列表和其他文字元素,使其非常適合內容分析和搜尋索引。
開始從DOCX文件提取文字的基本步驟是什麼?
首先透過NuGet(Install-Package IronWord)安裝IronWord,然後建立或載入一個WordDocument,最後使用ExtractText()方法從整個文件、特定段落或表格單元格中檢索文字。
文字提取適合用於建立文件索引系統嗎?
是的,IronWord的文字提取功能非常適合用於建立文件索引系統、內容管理解決方案和資料提取管道,提供對Word文件內容的高效程式化存取。
How can I export extracted text to a file using IronWord?
Once you've extracted the text, you can use C# file handling methods like `File.WriteAllText` to save the text to a file, as demonstrated in the tutorial with paragraph extraction where combined text is saved to an output.txt file.
Is it possible to extract introduction and conclusion sections separately?
Yes, you can extract specific paragraphs such as the introduction and conclusion by targeting them within the `Paragraphs` collection. This method is effective for document summarization tools requiring distinct extraction of key sections.
What encoding considerations should be taken when saving extracted text?
IronWord users should be aware of character encoding, especially when dealing with documents containing special characters or multiple languages. Ensuring the correct encoding is crucial when saving extracted text to maintain text integrity.
How can IronWord's text extraction be optimized for large documents?
For large documents, optimize text extraction by targeting only the necessary portions instead of extracting the entire content. This ensures efficiency, reduces processing time, and leverages the full capabilities of IronWord.
