为有中文需求的客户提供多渠道中文技术支持.

Wed Dec 04, 2024 3:19 am

我尝试使用下面的代码获取文本,但是获取到的文本不全,请问是还有什么遗漏吗
Code: Select all
    /// <summary>
    /// 获取PPT中的文字
    /// </summary>
    /// <param name="file"></param>
    /// <returns></returns>
    private static string ReadPptText(string file)
    {
        var presentation = new Presentation();
        presentation.LoadFromFile(file);
        var allText = new StringBuilder();
        foreach (ISlide slide in presentation.Slides)
        {
            foreach (IShape shape in slide.Shapes)
            {
                switch (shape)
                {
                    // 检查形状是否包含文本
                    case IAutoShape { TextFrame: not null } autoShape:
                        {
                            // 提取文本
                            foreach (TextParagraph paragraph in autoShape.TextFrame.Paragraphs)
                            {
                                foreach (Spire.Presentation.TextRange textRange in paragraph.TextRanges)
                                {
                                    allText.Append(textRange.Text);
                                }
                            }
                           
                            break;
                        }
                    // 获取表格中的文字
                    case ITable table:
                        {
                            for (var i = 0; i < table.TableRows.Count; i++)
                            {
                                for (var j = 0; j < table.ColumnsList.Count; j++)
                                {
                                    Spire.Presentation.Cell cell = table.TableRows[i][j];
                                    foreach (Spire.Presentation.TextParagraph paragraph in cell.TextFrame.Paragraphs)
                                    {
                                        foreach (Spire.Presentation.TextRange textRange in paragraph.TextRanges)
                                        {
                                            allText.Append(textRange.Text);
                                        }
                                    }
                                }
                            }

                            break;
                        }
                    case ISmartArt smartArt:
                    {
                        for (var i = 0; i < smartArt.Nodes.Count; i++)
                        {
                            allText.Append(smartArt.Nodes[i].TextFrame.Text);
                        }
                        break;
                    }
                }
            }

            // PPT换页则换行
            allText.AppendLine();
        }

        presentation.Dispose();
        return allText.ToString();
    }

leehao001
 
Posts: 7
Joined: Thu Nov 14, 2024 6:41 am

Wed Dec 04, 2024 7:00 am

您好,

感谢您的咨询。

您的代码中没有覆盖到获取组合图形中的文本情况。
Code: Select all
  if (shape is GroupShape)
 {
    //Find the shape group
     GroupShape groupShape = shape as GroupShape;
     foreach (IShape gShape in groupShape.Shapes)
      {
      }
}

您可以试下下面的方案:
Code: Select all
   
            //Create a PPT document and load file
            Presentation ppt = new Presentation();
            ppt.LoadFromFile(@"..\..\..\..\..\..\Data\GetSlideText.pptx");

            //Foreach the slide and get text
            foreach (ISlide slide in ppt.Slides)
            {
                ArrayList arrayList = slide.GetAllTextFrame();
                foreach (String text in arrayList)
                {
                    //...
                }
            }


如果还存在问题,请提供您的PPT文件,以便我们进行深入的调查。

Sincerely,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2900
Joined: Wed Jun 27, 2012 8:50 am

Tue Dec 10, 2024 3:50 am

您好,

请问您的问题解决了吗?

期待尽快收到你的反馈。

Sincerely,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2900
Joined: Wed Jun 27, 2012 8:50 am

Thu Dec 12, 2024 2:17 am

非常感谢您的帮助,我刚才试了一下,问题得到了成功解决

leehao001
 
Posts: 7
Joined: Thu Nov 14, 2024 6:41 am

Thu Dec 12, 2024 2:57 am

非常感谢您的反馈。

如果再需要帮助或遇到问题,请随时联系我们。

祝您生活愉快!

Sincerely,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2900
Joined: Wed Jun 27, 2012 8:50 am

Return to 中文技术支持