如何从C#中的PPT文件中提取字体颜色?

如何从C#中的PPT文件中提取字体颜色?,c#,powerpoint,openxml,openxml-sdk,C#,Powerpoint,Openxml,Openxml Sdk,我试图从一个PPTx文件中提取字体的颜色,我是一个使用OpenXMLSDK的初学者。我的目标是提取字体颜色,替换颜色并保存文件。以下是我到目前为止所做的尝试 using Drawing = DocumentFormat.OpenXml.Drawing; public static void SetPPTShapeColor(string docName) { using (PresentationDocument ppt = PresentationDocument.Op

我试图从一个PPTx文件中提取字体的颜色,我是一个使用OpenXMLSDK的初学者。我的目标是提取字体颜色,替换颜色并保存文件。以下是我到目前为止所做的尝试

using Drawing = DocumentFormat.OpenXml.Drawing;
public static void SetPPTShapeColor(string docName)
    {
        using (PresentationDocument ppt = PresentationDocument.Open(docName, true))
        {
            // Get the relationship ID of the first slide.
            PresentationPart part = ppt.PresentationPart;
            OpenXmlElementList slideIds = part.Presentation.SlideIdList.ChildElements;
            string relId = (slideIds[0] as SlideId).RelationshipId;
            Console.WriteLine(relId);

            // Get the slide part from the relationship ID.
            SlidePart slidepart = (SlidePart)part.GetPartById(relId);
            
            
            if (slidepart != null && slidepart.Slide!=null)
            {
                // Get the shape tree that contains the shape to change
                ShapeTree tree = slidepart.Slide.CommonSlideData.ShapeTree;


                // Get the first shape in the shape tree.
                Drawing.Shape shape = tree.GetFirstChild<Drawing.Shape>();
               
                  if (shape != null)
                  {

                    // Get the style of the shape.
                    Drawing.ShapeStyle style = shape.ShapeStyle;

                    // Get the fill reference.
                    Drawing.FillReference fillRef = style.FillReference;

                    // Set the fill color to SchemeColor Accent 6;
                    fillRef.SchemeColor = new Drawing.SchemeColor();
                    fillRef.SchemeColor.Val = Drawing.SchemeColorValues.Accent6;

                    // Save the modified slide.
                    slidepart.Slide.Save();
                } 
            }

        }
        
    }
使用Drawing=DocumentFormat.OpenXml.Drawing;
公共静态void SetPPTShapeColor(字符串docName)
{
使用(PresentationDocument ppt=PresentationDocument.Open(docName,true))
{
//获取第一张幻灯片的关系ID。
PresentationPart=ppt.PresentationPart;
OpenXmlElementList slideIds=part.Presentation.SlideIdList.ChildElements;
字符串relId=(slideIds[0]作为SlideId).RelationshipId;
控制台写入线(relId);
//从关系ID获取幻灯片部分。
SlidePart SlidePart=(SlidePart)part.GetPartById(relId);
if(slidepart!=null&&slidepart.Slide!=null)
{
//获取包含要更改的形状的形状树
ShapeTree tree=slidepart.Slide.CommonSlideData.ShapeTree;
//获取形状树中的第一个形状。
Drawing.Shape=tree.GetFirstChild();
if(shape!=null)
{
//获取形状的样式。
Drawing.ShapeStyle style=shape.ShapeStyle;
//获取填充引用。
Drawing.FillReference fillRef=style.FillReference;
//将填充颜色设置为SchemeColor Accent 6;
fillRef.SchemeColor=新绘图.SchemeColor();
fillRef.SchemeColor.Val=Drawing.SchemeColorValues.6;
//保存修改后的幻灯片。
slidepart.Slide.Save();
} 
}
}
}

如果有人知道如何提取字体颜色,请告诉我。上面的代码似乎没有提取字体的颜色。

您的朋友是“OpenXML生产力工具”(可从Microsoft网站下载)。制作您感兴趣的PPTX副本,更改副本中的字体颜色,保存它,然后使用tools Diff功能打开这两个文件。它将为您指出正确的代码hi@Flydog57非常感谢!!!。它对我有用,很高兴它有用。把那工具放在身边。在使用OpenXMLSDK时,它是必不可少的。“OpenXML生产力工具”(可从Microsoft网站下载)是您的朋友。制作您感兴趣的PPTX副本,更改副本中的字体颜色,保存它,然后使用tools Diff功能打开这两个文件。它将为您指出正确的代码hi@Flydog57非常感谢!!!。它对我有用,很高兴它有用。把那工具放在身边。在使用OpenXMLSDK时,它是必不可少的。