Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ppt_parser.py 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # Licensed under the Apache License, Version 2.0 (the "License");
  2. # you may not use this file except in compliance with the License.
  3. # You may obtain a copy of the License at
  4. #
  5. # http://www.apache.org/licenses/LICENSE-2.0
  6. #
  7. # Unless required by applicable law or agreed to in writing, software
  8. # distributed under the License is distributed on an "AS IS" BASIS,
  9. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. # See the License for the specific language governing permissions and
  11. # limitations under the License.
  12. #
  13. from io import BytesIO
  14. from pptx import Presentation
  15. class RAGFlowPptParser(object):
  16. def __init__(self):
  17. super().__init__()
  18. def __extract(self, shape):
  19. if shape.shape_type == 19:
  20. tb = shape.table
  21. rows = []
  22. for i in range(1, len(tb.rows)):
  23. rows.append("; ".join([tb.cell(
  24. 0, j).text + ": " + tb.cell(i, j).text for j in range(len(tb.columns)) if tb.cell(i, j)]))
  25. return "\n".join(rows)
  26. if shape.has_text_frame:
  27. return shape.text_frame.text
  28. if shape.shape_type == 6:
  29. texts = []
  30. for p in sorted(shape.shapes, key=lambda x: (x.top // 10, x.left)):
  31. t = self.__extract(p)
  32. if t:
  33. texts.append(t)
  34. return "\n".join(texts)
  35. def __call__(self, fnm, from_page, to_page, callback=None):
  36. ppt = Presentation(fnm) if isinstance(
  37. fnm, str) else Presentation(
  38. BytesIO(fnm))
  39. txts = []
  40. self.total_page = len(ppt.slides)
  41. for i, slide in enumerate(ppt.slides):
  42. if i < from_page:
  43. continue
  44. if i >= to_page:
  45. break
  46. texts = []
  47. for shape in sorted(
  48. slide.shapes, key=lambda x: ((x.top if x.top is not None else 0) // 10, x.left)):
  49. txt = self.__extract(shape)
  50. if txt:
  51. texts.append(txt)
  52. txts.append("\n".join(texts))
  53. return txts