MsOffi...Surgeon(3pm)
๐ฉบโจ MsOffice::Word::Surgeon โ user contributed Perl documentation, section 3pm โจ๐ฉบ
hi bestie ๐ so this is the whole manual for MsOffice::Word::Surgeon, a Perl module that lets you do literal surgery on the insides of .docx files. yes it's giving scalpel. yes it's giving regex. let's go ๐ฆ
๐ธ NAME ๐ธ
MsOffice::Word::Surgeon โ tamper with the guts of Microsoft docx documents, with regexes ๐
๐ SYNOPSIS ๐
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
$surgeon->document->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);
like, look at that. the vibe is immaculate. ๐ซถ
๐ DESCRIPTION ๐
๐ง Purpose
This module supports a few operations for inspecting or modifying contents in Microsoft Word documents in .docx format โ therefore the name 'surgeon'. And no cap, 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 addresses 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"inMsOffice::Word::Surgeon::PackagePart; - pretty-printing the internal XML structure.
๐ฆ The .docx format
The format of Microsoft .docx documents is described in <http://www.ecma-international.org/publications/standards/Ecma-376.htm> and <http://officeopenxml.com/>. An excellent introduction can be found at <https://www.toptal.com/xml/an-informal-introduction-to-docx>. Another precious source of documentation is <http://officeopenxml.com/WPcontentOverview.php>. 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. so it's like, it only bothers with the parts it cares about and lets the rest chill ๐
๐ METHODS ๐
๐ Constructor
new
my $surgeon = MsOffice::Word::Surgeon->new(docx => $filename_or_filehandle);
# or simply : ->new($filename);
Builds a new surgeon instance, initialized with the contents of the given filename or filehandle. scrub in, bestie ๐งผ
๐ Accessors
docxโ Path to the.docxfile.zipโ Instance ofArchive::Zipassociated with this file.partsโ Hashref toMsOffice::Word::Surgeon::PackagePartobjects, keyed by their part name in the ZIP file. There is always a'document'part. Other parts may be headers, footers, footnotes or endnotes.documentโ Shortcut to$surgeon->part('document')โ theMsOffice::Word::Surgeon::PackagePartobject 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$surgeonobject and are automatically delegated to the"document"part :"contents","original_contents","indented_contents","plain_text","replace".headers
my @header_parts = $surgeon->headers;
Returns the ordered list of names of header members stored in the ZIP file.
footers
my @footer_parts = $surgeon->footers;
Returns the ordered list of names of footer members stored in the ZIP file.
๐ฅ Other methods
part
my $part = $surgeon->part($part_name);
Returns the MsOffice::Word::Surgeon::PackagePart object corresponding to the given part name.
all_parts_do
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);
xml_member
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.
save_as
$surgeon->save_as($docx_file_or_filehandle);
Writes the ZIP archive into the given file or filehandle.
overwrite
$surgeon->overwrite;
Writes the updated ZIP archive into the initial file. If the initial "docx" was given as a filehandle, use the "save_as" method instead. ๐ซก
new_revision
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 :
to_deleteโ The string of text to delete (usually this will be the"matched"argument passed to the replacement callback).to_insertโ The string of new text to insert.authorโ A short string that will be displayed by MsWord as the"author"of this revision.dateโ 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.runโ A reference to theMsOffice::Word::Surgeon::Runobject surrounding this revision. The formatting properties of that run will be copied into the"<w:r>"nodes of the deleted and inserted text fragments.xml_beforeโ 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. ๐
๐ SEE ALSO ๐
The <https://metacpan.org/pod/Document::OOXML> 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 :
- <https://docs.microsoft.com/en-us/office/open-xml/word-processing> โ The C# Open XML SDK from Microsoft
- <http://www.ericwhite.com/blog/open-xml-powertools-developer-center/> โ Additional functionalities built on top of the XML SDK.
- <https://poi.apache.org> โ An open source Java library from the Apache foundation.
- <https://www.docx4java.org/trac/docx4j> โ Another open source Java library, competitor to Apache POI.
- <https://phpword.readthedocs.io/en/latest/> โ A PHP library dealing not only with Microsoft OOXML documents but also with OASIS and RTF formats.
- <https://pypi.org/project/python-docx/> โ A Python library, documented at <https://python-docx.readthedocs.io/en/latest/>.
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. and honestly? that's the main character trait ๐
The MsOffice::Word::Template module relies on the present module, together with the Perl Template Toolkit, to implement a templating system for Word documents. ๐ธ
๐ AUTHOR ๐
Laurent Dami, <dami AT cpan DOT org<gt>
๐ COPYRIGHT AND LICENSE ๐
Copyright 2019-2024 by Laurent Dami.
This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.
and that's the whole thing bestie, no notes, periodt ๐ซถ this page came from the user-contributed Perl documentation, built with perl v5.40.1, dated 2025-10-05, and it's still filed under that iconic MsOffi...Surgeon(3pm) title ๐โจ go forth and do some surgery ๐ฉบ๐
๐ฆ where this page comes from
- ubuntu resolute: libmsoffice-word-surgeon-perl 2.11-1 ยท Artistic-2.0
- ubuntu stonking: libmsoffice-word-surgeon-perl 2.11-1 ยท Artistic-2.0
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms