Domanda

Voglio estrarre le informazioni sui punti elenco presenti nel documento Word.Voglio qualcosa del genere:Supponiamo che il testo seguente sia in un documento word:

Passaggi per avviare l'auto:

  • Porta aperta
  • Siediti dentro
  • Chiudere la porta
  • Inserire la chiave
  • eccetera.

Quindi voglio il mio file di testo come di seguito:

Passaggi per avviare l'auto:

<BULET>Apri la porta </BULET>

<BULET> Siediti all'interno </BULET>

<BULET> Chiudi la porta </BULET>

<BULET> Inserisci chiave </BULET>

<BULET> ecc.</BULET>

Sto usando il linguaggio C# per fare questo.

Posso estrarre paragrafi da un documento Word e scriverli direttamente in un file di testo con alcune informazioni di formattazione come se il testo è in grassetto o in corsivo, ecc.ma non so come estrarre queste informazioni sul proiettile.

Qualcuno può dirmi come farlo?

grazie in anticipo

È stato utile?

Soluzione 3

ho avuto la risposta.....

Per prima cosa stavo convertendo il documento in base ai paragrafi.Ma invece di ciò, se elaboriamo il file doc frase per frase, è possibile determinare se quella frase contiene punti elenco o qualsiasi tipo di forma o se quella frase fa parte di una tabella.Quindi, una volta ottenute queste informazioni, possiamo convertire la frase in modo appropriato.Se qualcuno ha bisogno del codice sorgente, posso condividerlo.

Altri suggerimenti

Puoi farlo leggendo ogni frase. doc.Frasi è un array di oggetti Range.Quindi puoi ottenere lo stesso oggetto Range da Paragraph.

        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);
        }

In paraNumber puoi ottenere il livello del paragrafo e in buttetStr puoi ottenere il punto elenco come stringa.

sto usando Questo Strumento OpenXMLPower di Eric White.È gratuito e disponibile nel pacchetto NUGet.puoi installarlo dal gestore pacchetti di Visual Studio.enter image description here

Ha fornito uno snippet di codice pronto per l'uso.Questo strumento mi ha fatto risparmiare molte ore.Di seguito è riportato il modo in cui ho personalizzato lo snippet di codice da utilizzare per le mie esigenze.Infatti puoi utilizzare questi metodi come nel tuo progetto.

 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)
    {
        .....
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top