workdevelop / presentation-replacer
此包用数据替换演示文稿中的预定义变量
v0.0.9
2021-03-12 11:35 UTC
Requires
- ext-dom: *
- ext-simplexml: *
- ext-zip: *
README
注释
此包用数据替换演示文稿中的预定义变量。此包不仅适用于激活演示文稿模板,也适用于任何包含预定义字符串打印_args的zip文件。
示例
example.pptx文件是模板,其中某些src文件有预定义变量
chart2_datatype1_0, chart2_datatype1_1, chart2_datatype1_2 等此类
准备
$createPresentation = new \PresentationReplacer\CreatePresentation(); $templatePath = __DIR__.'/../storage/example.pptx'; $resultPath = __DIR__.'/../storage/result/result'.date('Ymd_His').'.pptx'; $createPresentation->setTemplatePath($templatePath); $createPresentation->setResultPath($resultPath); $createPresentation->setVariableRegex('/(\{char.*?\})/');
获取演示文稿中的预定义变量
try { $allVariables = $createPresentation->getAllVariablesAtPresentation(); } catch (PresentationReplacer\PptException $exception) { //do something }
替换预定义变量并下载真实数据
try { $createPresentation->replaceVariables([ '{chart2_datatype1_0}' => 500, '{chart2_datatype1_1}' => 501 '{chart2_datatype1_2}' => 100 ]); } catch (PresentationReplacer\PptException $exception) { //do something } $createPresentation->download('ready_presentation.pptx');
获取演示文稿中的文件内容
try { $chartContent = $createPresentation->getFileContentByRelativePath('ppt/charts/chart1.xml'); } catch (\PresentationReplacer\PptException $exception) { //do something }
设置演示文稿中的文件内容
try { $createPresentation->setFileContentByRelativePath('ppt/charts/chart1.xml', $chartContent); } catch (\PresentationReplacer\PptException $exception) { //do something }
如果您需要通过路径替换文件,例如图像
您知道被替换的相对文件路径('ppt/media/image2.png')
并且您有新图像所在位置的绝对路径
$logoPath = __DIR__.'/../storage/image.png'; try { $createPresentation->setFileContentByRelativePath('ppt/media/image2.png', $logoPath); } catch (\PresentationReplacer\PptException $exception) { //do something }
如何准备演示文稿模板
即将推出