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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 __extract(self, shape):
  23. if shape.shape_type == 19:
  24. tb = shape.table
  25. rows = []
  26. for i in range(1, len(tb.rows)):
  27. rows.append("; ".join([tb.cell(
  28. 0, j).text + ": " + tb.cell(i, j).text for j in range(len(tb.columns)) if tb.cell(i, j)]))
  29. return "\n".join(rows)
  30. if shape.has_text_frame:
  31. return shape.text_frame.text
  32. if shape.shape_type == 6:
  33. texts = []
  34. for p in sorted(shape.shapes, key=lambda x: (x.top // 10, x.left)):
  35. t = self.__extract(p)
  36. if t:
  37. texts.append(t)
  38. return "\n".join(texts)
  39. def __call__(self, fnm, from_page, to_page, callback=None):
  40. ppt = Presentation(fnm) if isinstance(
  41. fnm, str) else Presentation(
  42. BytesIO(fnm))
  43. txts = []
  44. self.total_page = len(ppt.slides)
  45. for i, slide in enumerate(ppt.slides):
  46. if i < from_page:
  47. continue
  48. if i >= to_page:
  49. break
  50. texts = []
  51. for shape in sorted(
  52. slide.shapes, key=lambda x: ((x.top if x.top is not None else 0) // 10, x.left)):
  53. try:
  54. txt = self.__extract(shape)
  55. if txt:
  56. texts.append(txt)
  57. except Exception as e:
  58. logging.exception(e)
  59. txts.append("\n".join(texts))
  60. return txts