Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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