Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

ppt_parser.py 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 HuPptParser(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(0, j).text + ": " + tb.cell(i, j).text for j in range(len(tb.columns)) if tb.cell(i, j)]))
  24. return "\n".join(rows)
  25. if shape.has_text_frame:
  26. return shape.text_frame.text
  27. if shape.shape_type == 6:
  28. texts = []
  29. for p in shape.shapes:
  30. t = self.__extract(p)
  31. if t: texts.append(t)
  32. return "\n".join(texts)
  33. def __call__(self, fnm, from_page, to_page, callback=None):
  34. ppt = Presentation(fnm) if isinstance(
  35. fnm, str) else Presentation(
  36. BytesIO(fnm))
  37. txts = []
  38. self.total_page = len(ppt.slides)
  39. for i, slide in enumerate(ppt.slides[from_page: to_page]):
  40. texts = []
  41. for shape in slide.shapes:
  42. txt = self.__extract(shape)
  43. if txt: texts.append(txt)
  44. txts.append("\n".join(texts))
  45. return txts