是否可以使用phpword编辑现有的docx文件?
我想在我现有的docx文件中添加页脚文本。
有很多例子,但这些例子是从头开始创建doc文件而不是编辑文件 有人可以给我一个例子吗?谢谢
就像这样。
$ strace -f -eexecve gcc-ar rc foo.a /dev/null |& grep /ar
[pid 14485] execve("/usr/lib/gcc/x86_64-pc-linux-gnu/5.3.0/../../../../x86_64-pc-linux-gnu/bin/ar", [...]) = 0
$ strace -f -eexecve gcc-ar rc foo.a /dev/null -B/usr/bin |& grep /ar
[pid 14493] execve("/usr/bin/ar", [...]) = 0
$ strace -f -eexecve gcc-ar rc foo.a /dev/null -B/usr/x86_64-pc-linux-gnu/binutils-bin/2.26/ |& grep /ar
[pid 14500] execve("/usr/x86_64-pc-linux-gnu/binutils-bin/2.26/ar", [...]) = 0
答案 0 :(得分:3)
是的,你可以编辑。
来自文档
PHPWord是一个用纯PHP编写的库,它提供了一组 用于写入和读取不同文档文件格式的类。该 当前版本的PHPWord支持Microsoft Office Open XML(OOXML 或OpenXML),Office应用程序的OASIS开放文档格式 (OpenDocument或ODF)和RTF格式(RTF)。
实际上这是程序
从文档中,我找到了一个例子,
<?php
include_once 'Sample_Header.php';
// Template processor instance creation
echo date('H:i:s'), ' Creating new TemplateProcessor instance...', EOL;
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('resources/Sample_07_TemplateCloneRow.docx');
// Variables on different parts of document
$templateProcessor->setValue('weekday', htmlspecialchars(date('l'))); // On section/content
$templateProcessor->setValue('time', htmlspecialchars(date('H:i'))); // On footer
$templateProcessor->setValue('serverName', htmlspecialchars(realpath(__DIR__))); // On header
// Simple table
$templateProcessor->cloneRow('rowValue', 10);
$templateProcessor->setValue('rowValue#1', htmlspecialchars('Sun'));
$templateProcessor->setValue('rowValue#2', htmlspecialchars('Mercury'));
$templateProcessor->setValue('rowValue#3', htmlspecialchars('Venus'));
$templateProcessor->setValue('rowValue#4', htmlspecialchars('Earth'));
$templateProcessor->setValue('rowValue#5', htmlspecialchars('Mars'));
$templateProcessor->setValue('rowValue#6', htmlspecialchars('Jupiter'));
$templateProcessor->setValue('rowValue#7', htmlspecialchars('Saturn'));
$templateProcessor->setValue('rowValue#8', htmlspecialchars('Uranus'));
$templateProcessor->setValue('rowValue#9', htmlspecialchars('Neptun'));
$templateProcessor->setValue('rowValue#10', htmlspecialchars('Pluto'));
$templateProcessor->setValue('rowNumber#1', htmlspecialchars('1'));
$templateProcessor->setValue('rowNumber#2', htmlspecialchars('2'));
$templateProcessor->setValue('rowNumber#3', htmlspecialchars('3'));
$templateProcessor->setValue('rowNumber#4', htmlspecialchars('4'));
$templateProcessor->setValue('rowNumber#5', htmlspecialchars('5'));
$templateProcessor->setValue('rowNumber#6', htmlspecialchars('6'));
$templateProcessor->setValue('rowNumber#7', htmlspecialchars('7'));
$templateProcessor->setValue('rowNumber#8', htmlspecialchars('8'));
$templateProcessor->setValue('rowNumber#9', htmlspecialchars('9'));
$templateProcessor->setValue('rowNumber#10', htmlspecialchars('10'));
// Table with a spanned cell
$templateProcessor->cloneRow('userId', 3);
$templateProcessor->setValue('userId#1', htmlspecialchars('1'));
$templateProcessor->setValue('userFirstName#1', htmlspecialchars('James'));
$templateProcessor->setValue('userName#1', htmlspecialchars('Taylor'));
$templateProcessor->setValue('userPhone#1', htmlspecialchars('+1 428 889 773'));
$templateProcessor->setValue('userId#2', htmlspecialchars('2'));
$templateProcessor->setValue('userFirstName#2', htmlspecialchars('Robert'));
$templateProcessor->setValue('userName#2', htmlspecialchars('Bell'));
$templateProcessor->setValue('userPhone#2', htmlspecialchars('+1 428 889 774'));
$templateProcessor->setValue('userId#3', htmlspecialchars('3'));
$templateProcessor->setValue('userFirstName#3', htmlspecialchars('Michael'));
$templateProcessor->setValue('userName#3', htmlspecialchars('Ray'));
$templateProcessor->setValue('userPhone#3', htmlspecialchars('+1 428 889 775'));
echo date('H:i:s'), ' Saving the result document...', EOL;
$templateProcessor->saveAs('results/Sample_07_TemplateCloneRow.docx');
echo getEndingNotes(array('Word2007' => 'docx'));
if (!CLI) {
include_once 'Sample_Footer.php';
}
现在看看下面给出的代码行,如何访问现有的页眉和页脚
$headers = $section->getHeaders();
$header1 = $headers[1]; // note that the first index is 1 here (not 0)
$elements = $header1->getElements();
$element1 = $elements[0]; // and first index is 0 here normally
// for example manipulating simple text information ($element1 is instance of Text object)
$element1->setText("adding text here - old part: " . $element1->getText());
$footers = $section->getFooters(); // to access footer
答案 1 :(得分:-1)
在文件(模板)docx的页脚中,您可以放置一个类似$ {var1}的变量。
使用&#34; TemplateProcessor&#34;。
打开文件作为模板$templateObject = new TemplateProcessor($filename);
替换文件中的var1变量
$templateObject->setValue('var1', 'test');
将模板转换为PhpWord
$fileName = $templateObject->save();
$phpWordObject = IOFactory::load($fileName);
unlink($fileName);
return $phpWordObject;
保存/渲染phpWord实例
// create the writer
$writer = $this->wordService->createWriter($phpWordObject, 'Word2007');
// create the response
$response = $this->wordService->createStreamedResponse($writer);
// adding headers
$dispositionHeader = $response->headers->makeDisposition(
ResponseHeaderBag::DISPOSITION_ATTACHMENT,
'export.docx'
);
$response->headers->set('Content-Type', 'application/msword');
$response->headers->set('Pragma', 'public');
$response->headers->set('Cache-Control', 'maxage=1');
$response->headers->set('Content-Disposition', $dispositionHeader);
答案 2 :(得分:-1)
可能对Laravel 5用户有用。您可以在控制器中使用它。
public function wordDocumentFromWordTemplate() {
$templateFile = public_path('templates') . '/template-file.docx';
$templateObject = new TemplateProcessor($templateFile);
$templateObject->setValue('var1', 'Text for var1');
$wordDocumentFile = $templateObject->save();
$headers = [
'Content-Type' => 'application/msword',
'Cache-Control' => 'max-age=0'
];
return response()->download($wordDocumentFile, 'result.docx', $headers);
}