PHPOffice / PHPPresentation

A pure PHP library for reading and writing presentations documents
https://phpoffice.github.io/PHPPresentation
Other
1.3k stars 519 forks source link

Reading and Replacing Text Using PhpPresentation. #367

Open himanshu-esfera opened 7 years ago

himanshu-esfera commented 7 years ago

Hi, I want to replace the keywords with the values in the ppt file using PhpPresentation . Unable to find a solution could you please help me. Thanks

Progi1984 commented 7 years ago

@himanshu-esfera I don't understand what you want. Could you define more precisely your feature ?

himanshu-esfera commented 7 years ago

I have a ppt file which contains the keywords like ${ppt_1}, ${ppt_2}, ${ppt_3} ....etc. these keywords are used in the ppt file. I want to replace them with there values. For eg : Hello ${ppt_1} is replaced by Hello Himanshu. Without Changing the style of the ppt.

himanshu-esfera commented 7 years ago

I have founded a solution but It is giving me the style error in some cases : Check my raw code

$pptReader = IOFactory::createReader('PowerPoint2007');
$oPHPPresentation = $pptReader->load('/var/www/html/3.ppt');
$property = $oPHPPresentation->getDocumentProperties();
$slides = $oPHPPresentation->getAllSlides();

foreach ($slides as $slide_k => $slide_v) {
  $shapes = $slides[$slide_k]->getShapeCollection();
  foreach ($shapes as $shape_k => $shape_v) {
    $shape = $shapes[$shape_k];
    echo get_class($shape).'</br>';

    if($shape instanceof PhpOffice\PhpPresentation\Shape\RichText){
       $paragraphs = $shapes[$shape_k]->getParagraphs();
       foreach ($paragraphs as $paragraph_k => $paragraph_v) {
         $text_elements = $paragraph_v->getRichTextElements();
         foreach ($text_elements as $text_element_k => $text_element_v) {
           $text = $text_element_v->getText();
           $new_text = str_replace('${name}', 'Esfera', $text);
           $text_element_v->setText($new_text);
         }
       }
    }
  }
}
$oWriterPPTX = IOFactory::createWriter($oPHPPresentation);
$oWriterPPTX->save("/var/www/html/other3.ppt");

Is there is any way to convert pptx/ppt to xml file. after that we will perform a string replace operation and again convert it to pptx/ppt.

ghost commented 6 years ago

@himanshu-esfera Did you find the final solution for this? I am also trying find a way to replace one string/Image with another

himanshu-esfera commented 6 years ago

Hello @xplorebitshq , After a long search, I have done it using Google Slide Service.

Here is my code : `$slidesService = $this->googleSlideConfig(); $service = $this->googleDriveConfig(); //GOOGLE DRIVE CONFIGURATION $file = new Google_Service_Drive_DriveFile(); $file->setName($file_name); $file->setMimeType('application/vnd.google-apps.presentation');

$result2 = $service->files->create( $file, array( 'data' => file_get_contents($dir), 'uploadType' => 'multipart' ) ); //CREATE FILE $file_id = $result2->id;

foreach ($merged_attributes as $attr_id => $attr_value) { $requests[] = new Google_Service_Slides_Request(array( 'replaceAllText' => array( 'containsText' => array( 'text' => $attr_value->syntax, 'matchCase' => true ), 'replaceText' => $replace = $attr_value->value ) )); }

$batchUpdateRequest = new Google_Service_Slides_BatchUpdatePresentationRequest(array( 'requests' => $requests )); $response = $slidesService->presentations->batchUpdate($file_id, $batchUpdateRequest);

$file = $service->files->export($file_id, 'application/vnd.openxmlformats-officedocument.presentationml.presentation', array('alt' => 'media' )); $content = $file->getBody()->getContents();

$file = $service->files->delete($file_id); //DELETE THE FILE FROM GOOGLE DRIVE file_put_contents($tempdir,$content); //REPLACE THE GOOGLE DRIVE FILE WITH LOCAL FILE. exit(); ` Thanks, Himanshu

ankitsam commented 6 years ago

@himanshu-esfera

Is there is any way to convert pptx/ppt to xml file. after that we will perform a string replace operation and again convert it to pptx/ppt.

pptx file is just a zip file, you can rename .pptx to .zip then directly unzip the .pptx file, there you will find xmls inside the ppt/slides folder.

saikksub commented 6 years ago

@ankitsam

pptx or docx files are in open XML format by default. In order to get all the XML files - just Unzip the .pptx file in the same working directory and you can access all the XML files and replace text in the same place holder.

I am working on a npm package that can replace text with placeholder for docx and pptx files.

EvelRus commented 3 years ago

Good afternoon everyone. Have an adequate way to replace a text template? $ {my_text_Welcome} -> "Welcome friend"

Thank you in advance!

stevo-king commented 3 years ago

Good afternoon everyone. Have an adequate way to replace a text template? $ {my_text_Welcome} -> "Welcome friend"

Thank you in advance!

patrickmau commented 2 years ago

Same question from my end. I know this is possible via setValue, setValues array and setComplexValue in phpword, would be great to have the same functionality in php-presentation.

Thanks for considering this for a future update.