Docxtemplater

 
 

テンプレートから Word DOCX を作成・管理する無料 Node.js ライブラリ

シンプルなテンプレートベースのアプローチと JavaScript API を使用して、Word DOCX、XLSX ドキュメントを動的に作成、編集、操作できる、業界トップクラスのオープンソース Node.js ライブラリです。

Docxtemplater とは?

今日のスピーディなデジタル社会では、効率性が最重要です。特に文書管理においては、契約書の作成、レポートの生成、パーソナライズされた手紙の作成など、ユーザーのニーズに応じて、文書作成を自動化する信頼できるツールが貴重な時間とリソースの節約になります。そこで登場するのが Docxtemplater です。テンプレートから Word 文書を生成するプロセスを簡素化する強力なライブラリです。このライブラリはテンプレートベースの文書生成を可能にし、Word 文書テンプレート内のプレースホルダーを実際のデータに置き換えて最終的な出力文書を作成します。

Docxtemplater の根幹は、データと事前定義されたテンプレートをマージすることで、ソフトウェア開発者が動的な Word、Excel、PowerPoint ドキュメントを作成・管理できる強力なオープンソース JavaScript ライブラリです。OpenXML 形式を活用し、ユーザーの文書の構造と内容を細かく制御でき、実行時に実際のデータに置き換えられるプレースホルダーを挿入可能にします。

Docxtemplater は多用途なライブラリで、ソフトウェア開発者が簡単に文書作成を自動化できます。テンプレートとデータマージの力を活用することで、文書ワークフローを効率化し、生産性を大幅に向上させます。ライブラリを正しく使用すれば、特定のニーズに合わせた動的な文書を簡単に生成できます。文書生成タスクの自動化やレポートの個別化など、Docxtemplater は強力なツールです。

Previous Next

Docxtemplater のインストール方法

Docxtemplater をインストールするには、JavaScript 用パッケージマネージャ npm を使用できます。以下のコマンドで正常にインストールしてください。

npm で Docxtemplater をインストール

 npm install --save docxtemplater pizzip

Node.js でテンプレートを使用した Word ドキュメント作成

オープンソースの Docxtemplater ライブラリは、テンプレートを使用して Microsoft Word DOCX ドキュメントを簡単に生成できるようにします。ソフトウェア開発者はキーとバリューのペアを含むオブジェクトを渡すことで、テンプレート内のプレースホルダーに動的データを注入し、最終的な出力ドキュメントを作成できます。以下の例は、既存のテンプレートをロードし、Node.js 環境内で Word ドキュメントを生成する方法を示しています。

Node.js 内でテンプレートから Word ドキュメントを生成する方法は?

const fs = require('fs');
const Docxtemplater = require('docxtemplater');

// Load the template
const content = fs.readFileSync('template.docx', 'binary');
const doc = new Docxtemplater(content);

// Set data to fill placeholders
const data = {
  firstName: 'John',
  lastName: 'Doe'
};

// Replace placeholders with actual data
doc.setData(data);

// Render the document
doc.render();

// Save the generated document
const output = fs.writeFileSync('output.docx', doc.getZip().generate({type: 'nodebuffer'}));

console.log('Document generated successfully!');

Node.js アプリでカスタム関数とフィルタを追加

Docxtemplater ライブラリのカスタム関数とフィルタは、テンプレート内でデータを操作し、特定の処理を実行する高度な機能を提供します。基本的なプレースホルダー置換を超えてライブラリの機能を拡張し、動的コンテンツ生成や複雑なデータ処理が可能になります。テンプレート内でデータを操作したり特定の処理を行うカスタム関数やフィルタを定義できます。以下の例は、開発者がショッピングカート内のアイテム合計価格を計算するカスタム関数と、通貨値をフォーマットするフィルタを実装する方法を示しています。

カスタム関数でショッピングカート内のアイテム価格を計算し、フィルタで通貨値をフォーマットする方法は?

const fs = require('fs');
const Docxtemplater = require('docxtemplater');

// Define custom function to calculate total price
function calculateTotal(items) {
  return items.reduce((total, item) => total + item.price * item.quantity, 0);
}

// Define custom filter to format currency
function formatCurrency(value) {
  return '$' + value.toFixed(2); // Format as dollars with 2 decimal places
}

// Load the template
const content = fs.readFileSync('template.docx', 'binary');
const doc = new Docxtemplater(content, {
  parser: {
    // Define custom tag for invoking functions
    getFunction: function(tag) {
      if (tag === 'calculateTotal') {
        return calculateTotal;
      }
    },
    // Define custom tag for applying filters
    getFilter: function(tag) {
      if (tag === 'currency') {
        return formatCurrency;
      }
    }
  }
});

// Set data with shopping cart items
const data = {
  items: [
    { name: 'Product 1', price: 10, quantity: 2 },
    { name: 'Product 2', price: 20, quantity: 1 },
    { name: 'Product 3', price: 15, quantity: 3 }
  ]
};

// Replace placeholders with actual data
doc.setData(data);

// Render the document
doc.render();

// Save the generated document
const output = fs.writeFileSync('output.docx', doc.getZip().generate({ type: 'nodebuffer' }));

console.log('Document generated successfully!');

リッチフォーマットのサポート

オープンソースの Docxtemplater は、シンプルなテンプレートベースのアプローチを提供することで、Word ドキュメントの作成と管理のプロセスを簡素化します。Word ドキュメントにおけるリッチフォーマットのサポートとは、フォントスタイル、色、サイズ、配置などさまざまなスタイリング属性を文書内の要素に適用できる機能を指します。テキスト、表、画像、段落、セクション、リスト、箇条書きなどのフォーマットを行い、読みやすさと視覚的魅力を高めます。以下の簡単な例は、文書内のテキストの一部にリッチフォーマットを適用する方法を示しています。

Node.js アプリケーションで Docxtemplater を使用してテキストにリッチフォーマットを適用する方法は?

// Render the document with custom parser options for rich formatting
doc.render({
  parser: {
    // Custom tag for interpreting HTML-like tags for rich formatting
    tagToken: function(tag) {
      return {
        tagName: tag.substring(1),
        mode: 'open'
      };
    },
    // Apply rich formatting based on tag names
    commands: {
      b: function(scope, context, tag) {
        return {
          type: 'applyRichText',
          value: { b: true }
        };
      },
      i: function(scope, context, tag) {
        return {
          type: 'applyRichText',
          value: { i: true }
        };
      },
      u: function(scope, context, tag) {
        return {
          type: 'applyRichText',
          value: { u: true }
        };
      },
      strike: function(scope, context, tag) {
        return {
          type: 'applyRichText',
          value: { strike: true }
        };
      }
    }
  }
});

// Save the generated document
const output = fs.writeFileSync('output.docx', doc.getZip().generate({ type: 'nodebuffer' }));

console.log('Document generated successfully!');

 日本