代码之家  ›  专栏  ›  技术社区  ›  Shekhar

如何从word文档中提取项目符号信息?

  •  3
  • Shekhar  · 技术社区  · 16 年前

    我想提取word文档中的项目符号信息。 我想要这样的东西: 假设以下文本在word文档中:

    启动汽车的步骤:

    • 开门
    • 坐在里面
    • 把门关上
    • 插入密钥

    启动汽车的步骤:

    <BULET>开门</BULET>

    <BULET>坐在里面</BULET>

    <BULET>关上门</BULET>

    <

    <

    我可以从word文档中提取段落,并直接将它们写入文本文件中,其中包含一些格式信息,如文本是粗体还是斜体等,但不知道如何提取此项目符号信息。

    提前谢谢

    3 回复  |  直到 16 年前
        1
  •  1
  •   Barun    14 年前

    你可以通过读每个句子来做到。 是范围对象的数组。所以你们可以从段落中得到相同的范围对象。

            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中可以得到bullet作为字符串。

        2
  •  1
  •   kumar chandraketu    9 年前

    我正在使用 this Eric White的OpenXMLPower工具。它是免费的,可在NUGet软件包。您可以从visualstudio包管理器安装它。 enter image description here

    他提供了一个现成的代码片段。这个工具节省了我很多时间。下面是我定制代码片段以满足需求的方法。 实际上,您可以在项目中使用这些方法。

     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("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + (listItem == "" ? "&bull;" : listItem) + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + itemText + "<br/>");
                        }
                    }
                    else if (null != listItem)
                    {
                        //If bullet character is found, replace it with equivalent html encoded character  
                        textItemSB.Append(listItem == "" ? "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&bull;" : 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)
        {
            .....
        }
    }
    
        3
  •  -4
  •   yhw42 menu_on_top    15 年前

    首先我是在段落的基础上转换文档。但如果我们逐句处理doc文件,则可以确定该语句是否包含项目符号或任何形状,或者该语句是否是表的一部分。一旦我们得到这些信息,我们就可以适当地转换这个句子。如果有人需要源代码,我可以分享。