The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

MsOffice::Word::Template - generate Microsoft Word documents from Word templates

SYNOPSIS

  my $template = MsOffice::Word::Template->new($filename);
  my $new_doc  = $template->process(\%data);
  $new_doc->save_as($path_for_new_doc);

DESCRIPTION

Purpose

This module treats a Microsoft Word document as a template for generating other documents. The idea is similar to the "mail merge" functionality in Word, but with much richer possibilities, because the whole power of a Perl templating engine can be exploited, for example for

  • dealing with complex, nested datastructures

  • using control directives for loops, conditionals, subroutines, etc.

Template authors just use the highlighing function in MsWord to mark the templating directives :

  • fragments highlighted in yelllow are interpreted as data directives, i.e. the template result will be inserted at that point in the document, keeping the current formatting properties (bold, italic, font, etc.).

  • fragments highlighted in green are interpreted as control directives that do not directly generate content, like loops, conditionals, etc. Paragraphs or table rows around such directives are dismissed, in order to avoid empty paragraphs or empty rows in the resulting document.

The syntax of data and control directives depends on the backend templating engine. The default engine is the Perl Template Toolkit; other engines can be specified as subclasses -- see the "TEMPLATE ENGINE" section below.

Status

This second release is a major refactoring of the first version, together with a refactoring of MsOffice::Word::Surgeon. New features include support for headers and footers and for image insertion. The internal object-oriented structure has been redesigned.

This module has been used successfully for a pilot project in my organization, generating quite complex documents from deeply nested datastructures. Yet this has not been used yet at large scale in production, so it is quite likely that some youth defects may still be discovered. If you use this module, please keep me informed of your difficulties, tricks, suggestions, etc.

METHODS

new

  my $template = MsOffice::Word::Template->new($docx);
  # or : my $template = MsOffice::Word::Template->new($surgeon);   # an instance of MsOffice::Word::Surgeon
  # or : my $template = MsOffice::Word::Template->new(docx => $docx, %options);

In its simplest form, the constructor takes a single argument which is either a string (path to a docx document), or an instance of MsOffice::Word::Surgeon. Otherwise the constructor takes a list of named parameters, which can be

docx

path to a MsWord document in docx format. This will automatically create an instance of MsOffice::Word::Surgeon and pass it to the constructor through the surgeon keyword.

surgeon

an instance of MsOffice::Word::Surgeon. This is a mandatory parameter, either directly through the surgeon keyword, or indirectly through the docx keyword.

data_color

the Word highlight color for marking data directives (default : yellow)

control_color

the Word highlight color for marking control directives (default : green). Such directives should produce no content. They are treated outside of the regular text flow.

In addition to the attributes above, other attributes can be passed to the constructor for specifying a templating engine different from the default Perl Template Toolkit. These are described in section "TEMPLATE ENGINE" below.

process

  my $new_doc = $template->process(\%data);
  $new_doc->save_as($path_for_new_doc);

Processes the template on a given data tree, and returns a new document (actually, a new instance of MsOffice::Word::Surgeon). That document can then be saved using "save_as" in MsOffice::Word::Surgeon.

AUTHORING TEMPLATES

A template is just a regular Word document, in which the highlighted fragments represent templating directives.

The data directives, i.e. the "holes" to be filled must be highlighted in yellow. Such zones must contain the names of variables to fill the holes. If the template engine supports it, names of variables can be paths into a complex datastructure, with dots separating the levels, like foo.3.bar.-1 -- see "GET" in Template::Manual::Directive and Template::Manual::Variables if you are using the Template Toolkit.

Control directives like IF, FOREACH, etc. must be highlighted in green. When seeing a green zone, the system will remove XML markup for the surrounding text and run nodes. If the directive is the only content of the paragraph, then the paragraph node is also removed. If this occurs within the first cell of a table row, the markup for that row is also removed. This mechanism ensures that the final result will not contain empty paragraphs or empty rows at places corresponding to control directives.

In consequence of this distinction between yellow and green highlights, templating zones cannot mix data directives with control directives : a data directive within a green zone would generate output outside of the regular XML flow (paragraph nodes, run nodes and text nodes), and therefore MsWord would generate an error when trying to open such content. There is a workaround, however : data directives within a green zone will work if they also generate the appropriate markup for paragraph nodes, run nodes and text nodes; but in that case you must also apply the "none" filter from Template::AutoFilter so that angle brackets in XML markup do not get translated into HTML entities.

See also MsOffice::Word::Template::Engine::TT2 for additional advice on authoring templates based on the Template Toolkit.

TEMPLATE ENGINE

This module invokes a backend templating engine for interpreting the template directives. The default engine is MsOffice::Word::Template::Engine::TT2, built on top of Template Toolkit. Another engine supplied in this distribution is MsOffice::Word::Template::Engine::Mustache, mostly as an example. To implement another engine, just subclass MsOffice::Word::Template::Engine.

To use an engine different from the default, the following arguments must be supplied to the "new" method :

engine_class

The name of the engine class. If the class is within the MsOffice::Word::Template::Engine namespace, just the suffix is sufficient; otherwise, specify the fully qualified class name.

engine_args

An optional list of parameters that may be used for initializing the engine

The engine will get a compile_template method call for each part in the .docx document (main

Given a datatree in $vars, the engine will be called as :

The engine must make sure that ampersand characters and angle brackets are automatically replaced by the corresponding HTML entities (otherwise the resulting XML would be incorrect and could not be opened by Microsoft Word). The Mustache engine does this automatically. The Template Toolkit would normally require to explicitly add an html filter at each directive :

  [% foo.bar | html %]

but thanks to the Template::AutoFilter module, this is performed automatically.

This module invokes a backend templating engine for interpreting the template directives. The default engine is MsOffice::Word::Template::Engine::TT2, built on top of Template Toolkit. Another engine supplied in this distribution is MsOffice::Word::Template::Engine::Mustache, mostly as an example. To implement another engine, just subclass MsOffice::Word::Template::Engine.

To use an engine different from the default, the following arguments must be supplied to the "new" method :

engine_class

The name of the engine class. If the class sits within the MsOffice::Word::Template::Engine namespace, just the suffix is sufficient; otherwise, specify the fully qualified class name.

engine_args

An optional list of parameters that may be used for initializing the engine

After initialization the engine will receive a compile_template method call for each part in the .docx document, i.e. not only the main document body, but also headers and footers.

Then the main process() method, given a datatree in $vars, will call the engine's process() method on each document part.

The engine must make sure that ampersand characters and angle brackets are automatically replaced by the corresponding HTML entities (otherwise the resulting XML would be incorrect and could not be opened by Microsoft Word). The Mustache engine does this automatically. The Template Toolkit would normally require to explicitly add an html filter at each directive :

  [% foo.bar | html %]

but thanks to the Template::AutoFilter module, this is performed automatically.

TROUBLESHOOTING

If a document generated by this module cannot open in Word, it is probably because the XML generated by your template is not equilibrated and therefore not valid. For example a template like this :

  This paragraph [[ IF condition ]]
     may have problems
  [[END]]

is likely to generate incorrect XML, because the IF statement starts in the middle of a paragraph and closes at a different paragraph -- therefore when the condition evaluates to false, the XML tag for closing the initial paragraph will be missing.

Compound directives like IF .. END, FOREACH .. END, TRY .. CATCH .. END should therefore be equilibrated, either all within the same paragraph, or each directive on a separate paragraph. Examples like this should be successful :

  This paragraph [[ IF condition ]]has an optional part[[ ELSE ]]or an alternative[[ END ]].
  
  [[ SWITCH result ]]
  [[ CASE 123 ]]
     Not a big deal.
  [[ CASE 789 ]]
     You won the lottery.
  [[ END ]]

AUTHOR

Laurent Dami, <dami AT cpan DOT org<gt>

COPYRIGHT AND LICENSE

Copyright 2020-2022 by Laurent Dami.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.