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


MsOffice::Word::Surgeon - tamper with the guts of Microsoft docx documents, with regexes


  my $surgeon = MsOffice::Word::Surgeon->new(docx => $filename);

  # extract plain text
  my $main_text    = $surgeon->document->plain_text;
  my @header_texts = map {$surgeon->part($_)->plain_text} $surgeon->headers;

  # unlink fields

  # reveal bookmarks
  $surgeon->document->reveal_bookmarks(color => 'cyan');

  # anonymize
  my %alias = ('Claudio MONTEVERDI' => 'A_____', 'Heinrich SCHÜTZ' => 'B_____');
  my $pattern = join "|", keys %alias;
  my $replacement_callback = sub {
    my %args =  @_;
    my $replacement = $surgeon->new_revision(to_delete  => $args{matched},
                                             to_insert  => $alias{$args{matched}},
                                             run        => $args{run},
                                             xml_before => $args{xml_before},
    return $replacement;
  $surgeon->all_parts_do(replace => qr[$pattern], $replacement_callback);

  # save the result
  $surgeon->overwrite; # or ->save_as($new_filename);



This module supports a few operations for inspecting or modifying contents in Microsoft Word documents in '.docx' format -- therefore the name 'surgeon'. Since a surgeon does not give life, there is no support for creating fresh documents; if you have such needs, use one of the other packages listed in the "SEE ALSO" section -- or use the companion module MsOffice::Word::Template.

Some applications for this module are :

  • content extraction in plain text format;

  • unlinking fields (equivalent of performing Ctrl-Shift-F9 on the whole document)

  • adding markers at bookmark start and end positions

  • regex replacements within text, for example for :

    • anonymization, i.e. replacement of names or adresses by aliases;

    • templating, i.e. replacement of special markup by contents coming from a data tree (see also MsOffice::Word::Template).

  • insertion of generated images (for example barcodes) -- see "images" in MsOffice::Word::Surgeon::PackagePart;

  • pretty-printing the internal XML structure.

The .docx format

The format of Microsoft .docx documents is described in and An excellent introduction can be found at Another precious source of documentation is Internally, a document is a zipped archive, where the member named word/document.xml stores the main document contents, in XML format.

Operating mode

The present module does not parse all details of the whole XML structure because it only focuses on text nodes (those that contain literal text) and run nodes (those that contain text formatting properties). All remaining XML information, for example for representing sections, paragraphs, tables, etc., is stored as opaque XML fragments; these fragments are re-inserted at proper places when reassembling the whole document after having modified some text nodes.




  my $surgeon = MsOffice::Word::Surgeon->new(docx => $filename);
  # or simply : ->new($filename);

Builds a new surgeon instance, initialized with the contents of the given filename.



Path to the .docx file


Instance of Archive::Zip associated with this file


Hashref to MsOffice::Word::Surgeon::PackagePart objects, keyed by their part name in the ZIP file. There is always a 'document' part. Other parts may be headers, footers, footnotes or endnotes.


Shortcut to $surgeon->part('document') -- the MsOffice::Word::Surgeon::PackagePart object corresponding to the main document. See the PackagePart documentation for operations on part objects. Besides, the following operations are supported directly as methods to the $surgeon object and are automatically delegated to the document part : contents, original_contents, indented_contents, plain_text, replace.


  my @header_parts = $surgeon->headers;

Returns the ordered list of names of header members stored in the ZIP file.


  my @footer_parts = $surgeon->footers;

Returns the ordered list of names of footer members stored in the ZIP file.

Other methods


  my $part = $surgeon->part($part_name);

Returns the MsOffice::Word::Surgeon::PackagePart object corresponding to the given part name.


  my $result = $surgeon->all_parts_do($method_name => %args);

Calls the given method on all part objects. Results are accumulated in a hash, with part names as keys to the results. This is mostly used to invoke the "replace" in MsOffice::Word::Surgeon::PackagePart method, i.e.

  $surgeon->all_parts_do(replace => qr[$pattern], $replacement_callback, %replacement_args);


  my $xml = $surgeon->xml_member($member_name); # reading
  # or
  $surgeon->xml_member($member_name, $new_xml); # writing

Reads or writes the given member name in the ZIP file, with utf8 decoding or encoding.



Writes the ZIP archive into the given file.



Writes the updated ZIP archive into the initial file.


  my $xml = $surgeon->new_revision(
    to_delete   => $text_to_delete,
    to_insert   => $text_to_insert,
    author      => $author_string,
    date        => $date_string,
    run         => $run_object,
    xml_before  => $xml_string,

This method is syntactic sugar for instantiating the MsOffice::Word::Surgeon::Revision class and returning XML markup for MsWord revisions (a.k.a. "tracked changes") generated by that class. Users can then manually review those revisions within MsWord and accept or reject them. This is best used in collaboration with the "replace" method : the replacement callback can call $self->new_revision(...) to generate revision marks in the document.

Either to_delete or to_insert (or both) must be present. Other parameters are optional. The parameters are :


The string of text to delete (usually this will be the matched argument passed to the replacement callback).


The string of new text to insert.


A short string that will be displayed by MsWord as the "author" of this revision.


A date (and optional time) in ISO format that will be displayed by MsWord as the date of this revision. The current date and time will be used by default.


A reference to the MsOffice::Word::Surgeon::Run object surrounding this revision. The formatting properties of that run will be copied into the <w:r> nodes of the deleted and inserted text fragments.


An optional XML fragment to be inserted before the <w:t> node of the inserted text

Operations on parts

See the MsOffice::Word::Surgeon::PackagePart documentation for other operations on package parts, including operations on fields, bookmarks or images.


The distribution on CPAN also manipulates docx documents, but with another approach : internally it uses XML::LibXML and XPath expressions for manipulating XML nodes. The API has some intersections with the present module, but there are also some differences : Document::OOXML has more support for styling, while MsOffice::Word::Surgeon has more flexible mechanisms for replacing text fragments.

Other programming languages also have packages for dealing with docx documents; here are some references :

The C# Open XML SDK from Microsoft

Additional functionalities built on top of the XML SDK.

An open source Java library from the Apache foundation.

Another open source Java library, competitor to Apache POI.

A PHP library dealing not only with Microsoft OOXML documents but also with OASIS and RTF formats.

A Python library, documented at

As far as I can tell, most of these libraries provide objects and methods that closely reflect the complete XML structure : for example they have classes for paragraphs, styles, fonts, inline shapes, etc.

The present module is much simpler but also much more limited : it was optimised for dealing with the text contents and offers no support for presentation or paging features. However, it has the rare advantage of providing an API for regex substitutions within Word documents.

The MsOffice::Word::Template module relies on the present module, together with the Perl Template Toolkit, to implement a templating system for Word documents.


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


Copyright 2019-2023 by Laurent Dami.

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