── goppt helpers ───────────────────────────────────────────────────────────── readPptxFile opens a .pptx file and returns its text content formatted as one section per slide.
(path string)
| 66 | // readPptxFile opens a .pptx file and returns its text content formatted |
| 67 | // as one section per slide. |
| 68 | func readPptxFile(path string) (string, error) { |
| 69 | reader := &ppt.PPTXReader{} |
| 70 | pres, err := reader.Read(path) |
| 71 | if err != nil { |
| 72 | return "", err |
| 73 | } |
| 74 | |
| 75 | slideCount := pres.GetSlideCount() |
| 76 | if slideCount == 0 { |
| 77 | return "", fmt.Errorf("the PowerPoint file contains no slides") |
| 78 | } |
| 79 | |
| 80 | var sb strings.Builder |
| 81 | fmt.Fprintf(&sb, "Total slides: %d\n", slideCount) |
| 82 | |
| 83 | for i := 0; i < slideCount; i++ { |
| 84 | slide, err := pres.GetSlide(i) |
| 85 | if err != nil { |
| 86 | continue |
| 87 | } |
| 88 | |
| 89 | name := slide.GetName() |
| 90 | if name == "" { |
| 91 | name = fmt.Sprintf("Slide %d", i+1) |
| 92 | } |
| 93 | fmt.Fprintf(&sb, "\n=== %s ===\n", name) |
| 94 | |
| 95 | for _, shape := range slide.GetShapes() { |
| 96 | text := extractShapeText(shape) |
| 97 | text = strings.TrimSpace(text) |
| 98 | if text != "" { |
| 99 | sb.WriteString(text) |
| 100 | sb.WriteByte('\n') |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | if notes := strings.TrimSpace(slide.GetNotes()); notes != "" { |
| 105 | fmt.Fprintf(&sb, "[Notes] %s\n", notes) |
| 106 | } |
| 107 | } |
| 108 | return sb.String(), nil |
| 109 | } |
| 110 | |
| 111 | // extractShapeText recursively extracts all text from a Shape. |
| 112 | func extractShapeText(shape ppt.Shape) string { |
no test coverage detected