如何从word文档中提取项目符号信息?
-
21-09-2019 - |
题
我想提取word文档中存在的项目符号信息。我想要这样的东西:假设下面的文本是在word文档中:
启动汽车的步骤:
- 开门
- 坐在里面
- 关门
- 插入钥匙
- ETC。
然后我想要我的文本文件如下所示:
启动汽车的步骤:
<BULET>开门</BULET>
<BULET>坐在里面</BULET>
<BULET>关上门</BULET>
<BULET>插入键</BULET>
<BULET>等</BULET>
我正在使用 C# 语言来执行此操作。
我可以从Word文档中提取段落并直接将它们写入文本文件中,并带有一些格式信息,例如文本是粗体还是斜体等。但不知道如何提取这个项目符号信息。
谁能告诉我该怎么做吗?
提前致谢
解决方案 3
我得到了答案......
首先,我在段落的基础上转换文档。但是,如果我们逐句处理 doc 文件,则可以确定该句子是否包含项目符号或任何类型的形状,或者该句子是否是表格的一部分。因此,一旦我们获得了这些信息,我们就可以适当地转换该句子。如果有人需要源代码,我可以分享。
其他提示
你可以通过阅读每个句子来做到这一点。 文档句子 是 Range 对象的数组。所以你可以从 Paragraph 中获得相同的 Range 对象。
foreach (Paragraph para in oDoc.Paragraphs)
{
string paraNumber = para.Range.ListFormat.ListLevelNumber.ToString();
string bulletStr = para.Range.ListFormat.ListString;
MessageBox.Show(paraNumber + "\t" + bulletStr + "\t" + para.Range.Text);
}
在 paraNumber 中,您可以获得段落级别,在 buttetStr 中,您可以获得字符串形式的项目符号。
我在用 这 Eric White 的 OpenXMLPower 工具。它是免费的,可通过 NUGet 包获取。您可以从 Visual Studio 包管理器安装它。
他提供了一个随时可用的代码片段。这个工具节省了我很多时间。以下是我定制的代码片段用于满足我的要求的方式。事实上,您可以在项目中使用这些方法。
private static WordprocessingDocument _wordDocument;
private StringBuilder textItemSB = new StringBuilder();
private List<string> textItemList = new List<string>();
/// Open word document using office SDK and reads all contents from body of document
/// </summary>
/// <param name="filepath">path of file to be processed</param>
/// <returns>List of paragraphs with their text contents</returns>
private void GetDocumentBodyContents()
{
string modifiedString = string.Empty;
List<string> allList = new List<string>();
List<string> allListText = new List<string>();
try
{
_wordDocument = WordprocessingDocument.Open(wordFileStream, false);
//RevisionAccepter.AcceptRevisions(_wordDocument);
XElement root = _wordDocument.MainDocumentPart.GetXDocument().Root;
XElement body = root.LogicalChildrenContent().First();
OutputBlockLevelContent(_wordDocument, body);
}
catch (Exception ex)
{
logger.Error("ERROR in GetDocumentBodyContents:" + ex.Message.ToString());
}
}
// This is recursive method. At each iteration it tries to fetch listitem and Text item. Once you have these items in hand
// You can manipulate and create your own collection.
private void OutputBlockLevelContent(WordprocessingDocument wordDoc, XElement blockLevelContentContainer)
{
try
{
string listItem = string.Empty, itemText = string.Empty, numberText = string.Empty;
foreach (XElement blockLevelContentElement in
blockLevelContentContainer.LogicalChildrenContent())
{
if (blockLevelContentElement.Name == W.p)
{
listItem = ListItemRetriever.RetrieveListItem(wordDoc, blockLevelContentElement);
itemText = blockLevelContentElement
.LogicalChildrenContent(W.r)
.LogicalChildrenContent(W.t)
.Select(t => (string)t)
.StringConcatenate();
if (itemText.Trim().Length > 0)
{
if (null == listItem)
{
// Add html break tag
textItemSB.Append( itemText + "<br/>");
}
else
{
//if listItem == "" bullet character, replace it with equivalent html encoded character
textItemSB.Append(" " + (listItem == "" ? "•" : listItem) + " " + itemText + "<br/>");
}
}
else if (null != listItem)
{
//If bullet character is found, replace it with equivalent html encoded character
textItemSB.Append(listItem == "" ? " •" : listItem);
}
else
textItemSB.Append("<blank>");
continue;
}
// If element is not a paragraph, it must be a table.
foreach (var row in blockLevelContentElement.LogicalChildrenContent())
{
foreach (var cell in row.LogicalChildrenContent())
{
// Cells are a block-level content container, so can call this method recursively.
OutputBlockLevelContent(wordDoc, cell);
}
}
}
if (textItemSB.Length > 0)
{
textItemList.Add(textItemSB.ToString());
textItemSB.Clear();
}
}
catch (Exception ex)
{
.....
}
}
不隶属于 StackOverflow