从 0.17.0 起,fsys.openXml.pptx.editor 可以按形状或占位符替换全部文字。适合固定模板和覆盖某个文本框;如果要只替换 {{Name}} 并保留其他运行格式,使用 replaceText。
先用 pptx.read(path,{includeStructure=true}) 定位实际页序、版式和 pages[].placeholders。同一编辑会话中,使用 doc.listPlaceholders(page) 查看最新形状,不用重新打开源件。形状 id 和占位符索引不是同一编号。图片占位符 type="pic" 不写文字,应使用 空图片占位符填充。
以下假定模板第 1 页已存在标题和索引 1 的内容占位符。运行后第 2 页是填充副本,示例页保留:
import fsys.openXml.pptx;
import fsys.openXml.pptx.editor;
var input="/template.pptx";
var before,err=fsys.openXml.pptx.read(input,{includeStructure=true;includeNotes=false});
if(!before) return null,err;
var slots=before.pages[[1]][["placeholders"]];
if(!#slots) return null,"第 1 页没有实际占位符";
var doc,err=fsys.openXml.pptx.editor.open(input);
if(!doc) return null,err;
var page,err=doc.copySlide(1);
if(!page){doc.close();return null,err;}
var n,err=doc.setPlaceholderText(page.index,"title","项目进展");
if(n===null){doc.close();return null,err;}
var n,err=doc.setPlaceholderText(page.index,{index=1},["完成结构读取","验证模板填充"]);
if(n===null){doc.close();return null,err;}
var path,err=doc.save("/filled-template.pptx");
doc.close();
if(!path) return null,err;
return fsys.openXml.pptx.read(path,{includeStructure=true;includeNotes=false});
doc.setShapeText(page,target,value,options):target 是数值形状 id、大小写敏感的唯一名称,或 {placeholder=selector}。doc.setPlaceholderText(page,selector,value,options) 是选择占位符的便捷入口。selector 为类型字符串或 {type,index}(至少指定其一)。title 匹配 title/ctrTitle,subtitle 匹配 subTitle,其余精确匹配。缺省 p:ph 类型按 obj 读,缺省索引按 0 读;body 不匹配 obj。有效的 reader shape.placeholder 可直接传入。页号从 1 开始,索引 0 有效;匹配多个时报错,不能选第一项,必要时用形状 id。p:sp。title/ctrTitle/subTitle/body/obj 占位符缺 txBody 时可补建;空主体也可填。不会从版式/母版创建实际形状;图片占位符、分组或 Fallback、特殊页脚/日期/页码占位符及歧义目标不写。表格走 setTableCell/setTableCells。value 为 UTF-8 字符串或连续字符串数组。CRLF、CR、LF 分段;VT('\v')是段内软换行。首尾空格、空段保留,""/[] 留一个空段,null 不是清空。成功返回新段落数,失败 null,err。bodyPr 和列表样式。新段落继承旧首段 pPr;新运行使用首段第一个具备 rPr 的运行(没有时用 endParaRPr)。后续段不重复设置首段的显式编号起点。并不逐段逐运行复制原来的混合格式,也不计算主题/版式继承效果。a:fld 默认拒绝,{fields="text"} 明确用静态文字替换整个主体(不计算域)。文字范围/逐段构建动画拒绝,原有整形状动画不重写。含未知文字主体/运行子内容时不强行改写。save 默认另存且不覆盖,源件与未修改部件原字节保留;close 释放会话。.potx 另存不会改变包主类型,不能靠改扩展名转成 .pptx。结构回读不判断视觉版式或放映,需用目标应用验收时只打开本任务样本。