2016-12-23 58 views
1

ユーザ入力に基づいていくつかの単語文書の内容を置き換える必要があります。 .docx(Word)ファイルの内容を読み込んで置換する

To, 
The Office, 
{officeaddress} 
Sub: Authorization Letter 
Sir/Madam, 

I/We hereby authorize to {Ename} whose signature is attested here below, to submit application and collect Residential permit for {name} 
Kindly allow him to support our International assignee 

{name}           {Ename} 

が方法はあります:私は、テンプレートファイル(例えば "template.docx")を読んで、そして

template.docxまず{fname}という名前、住所、{アドレス}などを交換しようとしていますLaravel 5.3でも同じことをしていますか?

私はphpwordでしようとしていますが、私は新しい単語ファイルを書くためのコードしか見ることができませんが、既存のものを読んだり置き換えたりすることはできません。また、私が単純に読み書きすると、書式設定がうまくいきません。

コード:

$file = public_path('template.docx'); 
$phpWord = \PhpOffice\PhpWord\IOFactory::load($file); 

$phpWord->save('b.docx'); 

b.docx

To, 
The Office, 
{officeaddress} 

Sub: 
Authorization Letter 
Sir/Madam, 


I/We hereby authorize 
to 

{Ename} 


whose signature is attested here below, to submit a 
pplication and collect Residential permit 
for 
{name} 

Kindly allow him to support our International assignee 


{name} 













{ 
E 
name} 
+0

このパッケージで実行できます:https://github.com/guiguidoc/laravel-phpword h ttps://github.com/PHPOffice/PHPWord –

+0

は、.docxファイルからの読み込みの例です。https://github.com/PHPOffice/PHPWord/blob/develop/samples/Sample_11_ReadWord2007.php –

+0

ありがとうございます - 私は彼らに試してみましょう。 –

答えて

0

DOCファイルからコンテンツを読み込むと交換するには、あなたがPHPWordパッケージを使用してcomposerコマンドを使用して、このパッケージをダウンロードすることができます

composer require phpoffice/phpword 

version v0.12.1によると、 PHPワードAutoloader.phpsrc/PHPWordフォルダからは、シングル

$template->setValue('variableName', 'MyVariableValue'); 

3)に置き換えた文字列のために、それは

require_once 'src/PhpWord/Autoloader.php'; 
\PhpOffice\PhpWord\Autoloader::register(); 

1)を開き、文書

$template = new \PhpOffice\PhpWord\TemplateProcessor('YOURDOCPATH'); 

2)置き換え文字列変数を登録します複数の出現のための変数
- あなたの配列

$template->cloneRow('arrayName', count($array)); 

のカウントにご使用のアレイプレースホルダを複製 -

for($number = 0; $number < count($array); $number++) { 
    $template->setValue('arrayName#'.($number+1), htmlspecialchars($array[$number], ENT_COMPAT, 'UTF-8')); 
} 

4変数の値を置き換えます)変更された文書

$template->saveAs('PATHTOUPDATED.docx'); 

UPDATEを保存
第3パラメータとしてlimitを$template->setValue($search, $replace, $limit)に渡すと、何回の一致が行われるかを指定できます。

+0

作業コードを共有できますか? –

+0

setValue()が常に動作しているとは限りません。書式設定は違いを生みますか?また、ドキュメントには、マクロのように変数が記述されています。 –

+0

AddWebのソリューションがまったく機能していません。エラーはありませんが、テンプレートは変更されずに新しい文書として保存されます。 代わりに、str_replaceで@SantoshAchariソリューションを使用しました。それにもかかわらず、まだ置き換えられていないプレースホルダがあります。それでもチェック理由。 – DvdEnde

2

これは、@ addweb-solution-pvt-ltdの答えに対する作業バージョンです。

//This is the main document in Template.docx file. 
$file = public_path('template.docx'); 

$phpword = new \PhpOffice\PhpWord\TemplateProcessor($file); 

$phpword->setValue('{name}','Santosh'); 
$phpword->setValue('{lastname}','Achari'); 
$phpword->setValue('{officeAddress}','Yahoo'); 

$phpword->saveAs('edited.docx'); 

{name}フィールドのすべてが変更されているわけではありません。理由は分かりません。また


は:

// Creating the new document... 
$zip = new \PhpOffice\PhpWord\Shared\ZipArchive(); 

//This is the main document in a .docx file. 
$fileToModify = 'word/document.xml'; 

$file = public_path('template.docx'); 
$temp_file = storage_path('/app/'.date('Ymdhis').'.docx'); 
copy($template,$temp_file); 

if ($zip->open($temp_file) === TRUE) { 
    //Read contents into memory 
    $oldContents = $zip->getFromName($fileToModify); 

    echo $oldContents; 

    //Modify contents: 
    $newContents = str_replace('{officeaddqress}', 'Yahoo \n World', $oldContents); 
    $newContents = str_replace('{name}', 'Santosh Achari', $newContents); 

    //Delete the old... 
    $zip->deleteName($fileToModify); 
    //Write the new... 
    $zip->addFromString($fileToModify, $newContents); 
    //And write back to the filesystem. 
    $return =$zip->close(); 
    If ($return==TRUE){ 
     echo "Success!"; 
    } 
} else { 
    echo 'failed'; 
} 

はうまく動作します。それを新しいファイルとして保存してダウンロードを強制する方法を見つけようとしています。

+0

あなたの解決策を見てください**更新** –

0

私はPHPでの.docまたは.docxのファイルを編集するには、同じタスクを持って、私はそれのために、このコードを使用しています。

リファレンスhttp://www.onlinecode.org/update-docx-file-using-php/

$full_path = 'template.docx'; 
    //Copy the Template file to the Result Directory 
    copy($template_file_name, $full_path); 

    // add calss Zip Archive 
    $zip_val = new ZipArchive; 

    //Docx file is nothing but a zip file. Open this Zip File 
    if($zip_val->open($full_path) == true) 
    { 
     // In the Open XML Wordprocessing format content is stored. 
     // In the document.xml file located in the word directory. 

     $key_file_name = 'word/document.xml'; 
     $message = $zip_val->getFromName($key_file_name);    

     $timestamp = date('d-M-Y H:i:s'); 

     // this data Replace the placeholders with actual values 
     $message = str_replace("{officeaddress}", "onlinecode org", $message); 
     $message = str_replace("{Ename}", "[email protected]", $message); 
     $message = str_replace("{name}", "www.onlinecode.org", $message); 

     //Replace the content with the new content created above. 
     $zip_val->addFromString($key_file_name, $message); 
     $zip_val->close(); 
    } 
0

あなたは簡単な解決策を見つけた場合、あなたがこのlibrary

例使用することができます。このコードは$に$検索を置き換えます を

$docx = new IRebega\DocxReplacer($pathToDocx); 

$docx->replaceText($search, $replace); 
ファイル$ pathToDocxに置き換えます
関連する問題