您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #
  2. # Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. import logging
  17. from io import BytesIO
  18. from pptx import Presentation
  19. class RAGFlowPptParser:
  20. def __init__(self):
  21. super().__init__()
  22. def __get_bulleted_text(self, paragraph):
  23. is_bulleted = bool(paragraph._p.xpath("./a:pPr/a:buChar")) or bool(paragraph._p.xpath("./a:pPr/a:buAutoNum")) or bool(paragraph._p.xpath("./a:pPr/a:buBlip"))
  24. if is_bulleted:
  25. return f"{' '* paragraph.level}.{paragraph.text}"
  26. else:
  27. return paragraph.text
  28. def __extract(self, shape):
  29. try:
  30. # First try to get text content
  31. if hasattr(shape, 'has_text_frame') and shape.has_text_frame:
  32. text_frame = shape.text_frame
  33. texts = []
  34. for paragraph in text_frame.paragraphs:
  35. if paragraph.text.strip():
  36. texts.append(self.__get_bulleted_text(paragraph))
  37. return "\n".join(texts)
  38. # Safely get shape_type
  39. try:
  40. shape_type = shape.shape_type
  41. except NotImplementedError:
  42. # If shape_type is not available, try to get text content
  43. if hasattr(shape, 'text'):
  44. return shape.text.strip()
  45. return ""
  46. # Handle table
  47. if shape_type == 19:
  48. tb = shape.table
  49. rows = []
  50. for i in range(1, len(tb.rows)):
  51. rows.append("; ".join([tb.cell(
  52. 0, j).text + ": " + tb.cell(i, j).text for j in range(len(tb.columns)) if tb.cell(i, j)]))
  53. return "\n".join(rows)
  54. # Handle group shape
  55. if shape_type == 6:
  56. texts = []
  57. for p in sorted(shape.shapes, key=lambda x: (x.top // 10, x.left)):
  58. t = self.__extract(p)
  59. if t:
  60. texts.append(t)
  61. return "\n".join(texts)
  62. return ""
  63. except Exception as e:
  64. logging.error(f"Error processing shape: {str(e)}")
  65. return ""
  66. def __call__(self, fnm, from_page, to_page, callback=None):
  67. ppt = Presentation(fnm) if isinstance(
  68. fnm, str) else Presentation(
  69. BytesIO(fnm))
  70. txts = []
  71. self.total_page = len(ppt.slides)
  72. for i, slide in enumerate(ppt.slides):
  73. if i < from_page:
  74. continue
  75. if i >= to_page:
  76. break
  77. texts = []
  78. for shape in sorted(
  79. slide.shapes, key=lambda x: ((x.top if x.top is not None else 0) // 10, x.left if x.left is not None else 0)):
  80. try:
  81. txt = self.__extract(shape)
  82. if txt:
  83. texts.append(txt)
  84. except Exception as e:
  85. logging.exception(e)
  86. txts.append("\n".join(texts))
  87. return txts