💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
# **模板处理** 您可以使用包含的搜索模式(宏)创建OOXML文档模板,该模板可以替换为您希望的任何值。只能替换单行值。宏定义如下:${search-pattern}。要加载模板文件,请创建TemplateProcessor的新实例。 `$ templateProcessor \= new TemplateProcessor(' Template.docx ');` ## **替换值** ### **setValue(设定值)** 给定一个包含的模板 你好$ {firstname} $ {lastname} ! ``` 下面将取代${firstname}同John,并${lastname}用Doe。生成的文档现在将包含Hello John Doe! $ templateProcessor \- > setValue(' firstname ',' John '); $ templateProcessor \- > setValue(' lastname ',' Doe '); ``` ### **setValues(数组)方法** PHPword 0.16版本中未加入此函数,更新至0.17-dev版本后可用。 您还可以通过将所有值传递到数组中来设置多个值。 ``` $ templateProcessor \- > setValues(array(' firstname ' \=> ' John ',' lastname ' \=> ' Doe ')); ``` ### **setImageValue(图片)** ***图像的搜索模式模型可以是:*** ``` · ${search-image-pattern} · ${search-image-pattern:\[width\]:\[height\]:\[ratio\]} · ${search-image-pattern:\[width\]x\[height\]} · ${search-image-pattern:size=\[width\]x\[height\]} · ${search-image-pattern:width=\[width\]:height=\[height\]:ratio=false} ``` ***where:*** · \[width\]和\[height\]可以只是带有小节的数字或数字,由Word支持(cm,mm,in,pt,pc,px,%,em,ex) · \[比率\]只使用用于false,\-或f以关闭图像的方面的宽高比。默认情况下,模板图像大小用作“容器”大小。 例: ``` $ {CompanyLogo} $ {UserLogo :50 :50 } $ {Name} \- $ {City} \- $ {Street} $ templateProcessor \= new TemplateProcessor(' Template.docx '); $ templateProcessor \- > setValue(' Name ',' John Doe '); $ templateProcessor \- > setValue(array(' City ',' Street '),array(' Detroit ',' 12th Street ')); $ templateProcessor \- > setImageValue(' CompanyLogo ',' path / to / company / logo.png '); $ templateProcessor \- > setImageValue(' UserLogo ',array(' path ' \=> ' path / to / logo.png ',' width ' \=> 100,' height ' \=> 100,' ratio ' \=> false)); ``` ## **块替换** ### **1.cloneBlock** 给出一个包含See的模板Sample\_23\_TemplateBlock.php示例。 ``` $ {block\_name} Customer: $ {customer\_name} Address: $ {customer\_address} $ { / block\_name} ``` 下面将复制之间的所有内容${block\_name},并${/block\_name}3次。 $ templateProcessor \- > cloneBlock(' block\_name ',3,true,true); 最后一个参数将重命名块中定义的任何宏,并将#1,#2,#3 ...添加到宏名称中。结果将是 ``` Customer: $ {customer\_name #1 } Address: $ {customer\_address #1 } Customer: $ {customer\_name #2 } Address: $ {customer\_address #2 } Customer: $ {customer\_name #3 } Address: $ {customer\_address #3 } ``` 也可以传递一个带有值的数组来替换marcros。如果传递带有替换的数组,count则忽略该参数,它是计数的数组的大小。 ``` $ replacements \= array( array(' customer\_name ' => ' Batman ',' customer\_address ' => ' Gotham City '),array(' customer\_name ' => ' Superman ',' customer\_address ' => ' Metropolis '),); $ templateProcessor \- > cloneBlock(' block\_name ', true,false,$ replacementments); ``` 结果将是 ``` Customer: Batman Address: Gotham City Customer: Superman Address: Metropolis ``` ### **2.replaceBlock** 给定一个包含的模板 `$ {block\_name}` 此块内容将被替换 `$ { / block\_name}` 以下内容将替换``$ {block\_name}``和${/block\_name}传递的值之间的所有内容。 `$ templateProcessor \- > replaceBlock(' block\_name ','这是替换文本。');` ### **3.deleteBlock** 与上一个相同,但它删除了块 `$ templateProcessor \- > deleteBlock(' block\_name ');` ## 表格行 ### **1.cloneRow** 克隆模板文档中的表格行。请参阅Sample\_07\_TemplateCloneRow.php示例。 ``` \+ ----------- + ---------------- + | $ {userId} | $ {userName} | | | ---------------- + | | $ {userAddress} | \+ ----------- + ---------------- + $ templateProcessor \- > cloneRow(' userId ',2); ``` 会导致 ``` \+ ------------- + ------------------ + | $ {userId #1 } | $ {userName #1 } | | | ------------------ + | | $ {userAddress #1 } | \+ ------------- + ------------------ + | $ {userId #2 } | $ {userName #2 } | | | ------------------ + | | $ {userAddress #2 } | \+ ------------- + ------------------ + ``` ### **2.cloneRowAndSetValues** 在$ search param标识的表行中查找一行,并将其克隆为$ values中的条目的次数。 ``` \+ ----------- + ---------------- + | $ {userId} | $ {userName} | | | ---------------- + | | $ {userAddress} |\+ ----------- + ---------------- + $ values \= \[ \[ ' userId ' \=> 1,' userName ' \=> ' Batman ',' userAddress ' \=> ' Gotham City ' \], \[ ' userId ' \=> 2,' userName ' \=> ' Superman ',' userAddress ' \=> ' Metropolis ' \], \]; $ templateProcessor \- >cloneRowAndSetValues(' userId ',); ``` 会导致 ``` +---+-------------+ | 1 | Batman | | |-------------+ | | Gotham City | +---+-------------+ | 2 | Superman | | |-------------+ | | Metropolis | +---+-------------+ ``` ## 页眉、页脚样式 ### **1.applyXslStyleSheet** 应用传递给页眉部分,页脚部分和主要部分的XSL样式表 ``` $ xslDomDocument \= new \\ DOMDocument(); $ xslDomDocument \- > load(' / path / to / my /stylesheet.xsl '); $ templateProcessor \- > applyXslStyleSheet($ xslDomDocument); ``` ### **2.setComplexValue** 使用ComplexType传递$ {macro}。请参阅Sample\_40_TemplateSetComplexValue.php示例。 ``` $ inline \= new TextRun(); $ inline \- > addText(' by a red italic text ',array(' italic ' \=> true,' color ' \=> ' red ')); $ templateProcessor \- > setComplexValue(' inline ',$ inline); ``` ### **3.setComplexBlock** 使用ComplexType传递$ {macro}。请参阅Sample\_40_TemplateSetComplexValue.php示例。 ``` $ table \= new Table(array(' borderSize ' \=> 12,' borderColor ' \=> ' green ',' width ' \=> 6000,' unit ' \=> TblWidth :: TWIP)); $ table \- > addRow(); $ table \- > addCell(150)\- > addText(' Cell A1 '); $ table \- >150)\- > addText(' Cell A2 '); $ table \- > addCell(150)\- > addText(' Cell A3 ');$ table \- > addRow(); $ table \- > addCell(150)\- > addText(' Cell B1 '); $ table \- > addCell(150)\- > addText(' Cell B2 '); $ table \- > addCell(150)\- > addText(' Cell B3 '); $ templateProcessor \- > setComplexBlock( ' table ', $ table); ```