Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature : add PPTX data reader #220

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ The first part of the flow is to read data from a source.
This can be a database, a csv file, a json file, a text file, a website, a pdf, a word document, an excel file, ...
The only requirement is that you can read the data and that you can extract the text from it.

For now we only support text files, pdf and docx but we plan to support other data type in the future.
For now we only support text files, pdf, pptx and docx but we plan to support other data type in the future.

You can use the [`FileDataReader`](src/Embeddings/DataReader/FileDataReader.php) class to read a file. It takes a path to a file or a directory as parameter.
The second optional parameter is the class name of the entity that will be used to store the embedding.
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"guzzlehttp/guzzle": "^7.4.5",
"guzzlehttp/psr7": "^2.7",
"openai-php/client": "^v0.7.7 || ^v0.8.4 || ^v0.9.2",
"phpoffice/phppresentation": "^1.1",
"phpoffice/phpword": "^1.2.0",
"psr/http-message": "^2.0",
"smalot/pdfparser": "^2.7"
Expand Down
6 changes: 6 additions & 0 deletions src/Embeddings/DataReader/FileDataReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ private function getContentFromFile(string $path): string|false
return $docxReader->getText($path);
}

if ($fileExtension === 'pptx') {
$pptxReader = new PptxReader();

return $pptxReader->getText($path);
}

return file_get_contents($path);
}

Expand Down
33 changes: 33 additions & 0 deletions src/Embeddings/DataReader/PptxReader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace LLPhant\Embeddings\DataReader;

use PhpOffice\PhpPresentation\Shape\RichText;
use PhpOffice\PhpPresentation\IOFactory;

class PptxReader
{
public function getText(string $path): string
{
$reader = IOFactory::createReader('PowerPoint2007');
$ppt = $reader->load($path);
$fullText = '';

foreach ($ppt->getAllSlides() as $slideIndex => $slide) {
$fullText .= "Slide " . ($slideIndex + 1) . ": ";
foreach ($slide->getShapeCollection() as $shape) {
if ($shape instanceof RichText) {
foreach ($shape->getParagraphs() as $paragraph) {
$text = '';
foreach ($paragraph->getRichTextElements() as $element) {
$text .= $element->getText();
}
$fullText .= $text . " ";
}
}
}
}

return $fullText;
}
}
1 change: 1 addition & 0 deletions tests/Unit/Embeddings/DataReader/FileDataReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
['document-with-text-breaks.docx', 'Sample document with text breaks'],
['simple_document_with_links.docx', 'This is a doc with links'],
['data-pdf.pdf', 'This data is from a pdf'],
['powerpoint.pptx', 'Slide 1'],
]);

it('can read pdf and texts ', function () {
Expand Down
Binary file not shown.
Loading