C#OpenXml获取DOCX WordStyle属性简化代码



只是好奇是否有一个更简化的版本来检查给定的正文是否应用了"Heading3"的单词样式,给定了我在学习OpenXML库时编写的这个示例C#代码。需要明确的是,我只是在问给定的body元素,我如何确定给定的body元素是否应用了什么单词样式。我最终不得不编写一个程序来处理大量的.DOCX文件,并需要自上而下地处理它们。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using System.IO;
namespace docxparsing
{
   class Program
   {
    static void Main()
    {
        string file_to_parse = @"C:tempsample.docx";
        WordprocessingDocument doc = WordprocessingDocument.Open(file_to_parse,false);
        Body body = doc.MainDocumentPart.Document.Body;
        string fooStr
        foreach( var foo in body )
        {
            fooStr = foo.InnerXml;
            /*
                            these 2 comments represent 2 different xml snippets from 'fooStr'. the only way i figure out how to get the word style is by reading
                                    this xml and doing checks for values. i don't know of any other approach in using the body element to check for the applied word style
                <w:pPr xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:pStyle w:val="Heading2" />
                <w:pPr xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:pStyle w:val="Heading3" />
            */
            bool hasHeading3 = fooStr.Contains("pStyle w:val="Heading3"");
            if ( hasHeading3 )
            {
                Console.WriteLine("heading3 found");
            }
        }
        doc.Close();
    }
}

}

//---------------------------------------------------------------------------------

编辑

以下是一种方法的更新代码。总体上仍然不满意,但它有效。要查看的函数是getWordStyleValue(字符串x)

using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using System;
using System.Diagnostics;
using System.IO;
using System.Text;

namespace docxparsing
{
    class Program
    {
        // ************************************************
        // grab the word style value
        // ************************************************
        static string getWordStyleValue(string x)
        {
            int p = 0;
            p = x.IndexOf("w:pStyle w:val=");
            if ( p == -1 )
            {
                return "";
            }
            p = p + 15;
            StringBuilder sb = new StringBuilder();
            while (true)
            {
                p++;
                char c = x[p];
                if (c != '"')
                {
                    sb.Append(c);
                }
                else
                {
                    break;
                }
            }
            string s = sb.ToString();
            return s;
        }

        // ************************************************
        // Main
        // ************************************************
        static void Main(string[] args)
        {
            string theFile = @"C:tempsample.docx";
            WordprocessingDocument doc =  WordprocessingDocument.Open(theFile,false);
            string body_table     = "DocumentFormat.OpenXml.Wordprocessing.Table";
            string body_paragraph = "DocumentFormat.OpenXml.Wordprocessing.Paragraph";
            Body body = doc.MainDocumentPart.Document.Body;
            StreamWriter sw1 = new StreamWriter("paragraphs.log");
            foreach (var b in body)
            {
                string body_type = b.ToString();
                if (body_type == body_paragraph)
                {
                    string str = getWordStyleValue(b.InnerXml);
                    if (str == "" || str == "HeadingNon-TOC" || str == "TOC1" || str == "TOC2" || str == "TableofFigures" || str == "AcronymList" )
                    {
                        continue;
                    }
                    sw1.WriteLine(str + "," + b.InnerText);
                }
                if ( body_type == body_table )
                {
             //       sw1.WriteLine("Table:n{0}",b.InnerText);
                }
            }
            doc.Close();
            sw1.Close();
        }
    }
}

是。你可以这样做:

bool ContainsHeading3 = body.Descendants<ParagraphSytleId>().Any(psId => psId.Val == "Heading3");

这将查看所有ParagraphStyleId元素(xml中的w:pStyle),并查看它们中的任何是否具有Heading3Val

只是从原始帖子中粘贴这个编辑,这样他就有了更好的可见性。

这是我想出的一个解决方案。是的,这有点过分(如果这是一个词的话),但使用LINQ(我最喜欢的)来优化一个更优雅的解决方案。

--

以下是一种方法的更新代码。总体上仍然不满意,但它有效。要查看的函数是getWordStyleValue(字符串x)

using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using System;
using System.Diagnostics;
using System.IO;
using System.Text;

namespace docxparsing
{
    class Program
    {
        // ************************************************
        // grab the word style value
        // ************************************************
        static string getWordStyleValue(string x)
        {
            int p = 0;
            p = x.IndexOf("w:pStyle w:val=");
            if ( p == -1 )
            {
                return "";
            }
            p = p + 15;
            StringBuilder sb = new StringBuilder();
            while (true)
            {
                p++;
                char c = x[p];
                if (c != '"')
                {
                    sb.Append(c);
                }
                else
                {
                    break;
                }
            }
            string s = sb.ToString();
            return s;
        }

        // ************************************************
        // Main
        // ************************************************
        static void Main(string[] args)
        {
            string theFile = @"C:tempsample.docx";
            WordprocessingDocument doc =  WordprocessingDocument.Open(theFile,false);
            string body_table     = "DocumentFormat.OpenXml.Wordprocessing.Table";
            string body_paragraph = "DocumentFormat.OpenXml.Wordprocessing.Paragraph";
            Body body = doc.MainDocumentPart.Document.Body;
            StreamWriter sw1 = new StreamWriter("paragraphs.log");
            foreach (var b in body)
            {
                string body_type = b.ToString();
                if (body_type == body_paragraph)
                {
                    string str = getWordStyleValue(b.InnerXml);
                    if (str == "" || str == "HeadingNon-TOC" || str == "TOC1" || str == "TOC2" || str == "TableofFigures" || str == "AcronymList" )
                    {
                        continue;
                    }
                    sw1.WriteLine(str + "," + b.InnerText);
                }
                if ( body_type == body_table )
                {
             //       sw1.WriteLine("Table:n{0}",b.InnerText);
                }
            }
            doc.Close();
            sw1.Close();
        }
    }
}

相关内容

  • 没有找到相关文章

最新更新