Powershell and Office Open XML Format Document Generation Sample
This is the Powershell sample I demoed at the Melbourne Victoria.NET User Group event in June. It's essentially a document generation sample and yup it really works and pretty darn quick. On a Dual Core 1.8gig machine, this Powershell script was generating 60 to 70 Office Open XML Documents a second and it was only running on one thread/core...
And yes you can doc generate Office Open XML documents on the server without requiring Office/Word to be installed and then run the WordML docs through something like https://www.renderx.com to generate PostScript for cheaper offsite printing, PDFs, XSLFOs etc..., all very cool and accessible!!
You'll find the sample at https://projectdistributor.net/Releases/Release.aspx?releaseId=418
Why Powershell and Office XML Format Doc Generation
- I wanted to play around with ideas for document generation that also had the flexibility of a script engine drive the process, potential ease of management etc...
- More likely scenario is that this script would be an extensibility point for an overall solution, not the solution itself... Check out the Windows PowerShell SDK for information on invoking Powershell scripts from an application.
- I wanted to learn how to load .NET assemblies in to Powershell, flow control etc...
- Stuff that is missing - lol, ok, this is a sample, it's missing Powershell Exception Handling
- I reckon this sample could go considerably faster, say 20 to 30%, I reload and crack open the template document for every customer record I process, it would make sense to load the template only once at initialisation, but for now I'm not going to optimise...
Sample Contents
The sample zip file at https://projectdistributor.net/Releases/Release.aspx?releaseId=418 contains the following files:-
- An Office Open XML Format document as a template, marked up with text controls and bound to XML Custom parts using the Word 2007 Content Control Toolkit.
- Check out Matthew Scott: Application Development using the Open XML File Formats for more info on using the Word 2007 Content Control Toolkit.
- A Customer.xml document, which contains stuff like first and last names, amount due, payment date etc, this could easy be data from a database.
- StandardText.xml - which has standard parts for a letter, terms and conditions, company name etc
- The C# version of the same scenario
- by default this samples runs out of the d:\dgDemo directory, tweak the script as required...
This is the general flow for the Powershell script...
- Initialise global variables: General initialization of globals happens at
- Initialise(): Load the StdTexts.xml in to a Hash table and load the customers.xml in to a DOM
- Main: Initiate the main customer processing loop
- ProcessDocs([int]$docNumber): Create a copy of the XML Doc and load the doc in to a DOM using System.IO.Packaging.Package and find the main document part in the XML Word doc
- GetDocumentPart($OfficeDocRel): From the document part, go find all the XML Custom Parts
- GetCustomXmlPart($customXmlRel): Now you've got a reference to the XML Custom Parts, loop through each element, use the Element name to look up the hash table for standard letter parts, if not in the hash table then check out the customer.xml dom for matching element/attribute name, if it exists insert the data in to the document
For more information on Powershell then check out
Cheers and enjoy Dave
BTW, if you think you'll use it or some derivation then let me know via my blog, just interested:)
=================================================================================================
# $psAppDomain = [AppDomain]::CurrentDomain
# $psAppDomain.GetAssemblies()
d:
cd \dgDemo
## Load additional required assemblies ##
$dummy = [System.Reflection.Assembly]::Load("Windowsbase, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")
## Initialise global variables ##
$documentType = "https://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument"
$customXmlType = "https://schemas.openxmlformats.org/officeDocument/2006/relationships/customXml"
[uri]$uri = $null; $dummy = [Uri]::TryCreate("/", [UriKind]::Relative, [ref]$uri)
$doc = new-object -type system.xml.xmldocument
$custRecord = $null
[string]$text = ""
[hashtable]$stdTexts = @{}
function GetCustomXmlPart($customXmlRel)
{
#Get CustomXML Part
$docPart = $Package.GetPart([System.IO.Packaging.PackUriHelper]::ResolvePartUri($uri, $customXmlRel.TargetUri))
$doc.load($docPart.getstream())
#This demo looks one level below the document element for element names to match either customer data or std texts
$docElementName = $doc.get_documentelement().get_name()
#loop through all the child elements in the customXML doc part looking for matches
$doc.$docElementName.get_childnodes() |
&{process{
$docElementName = $_.get_name()
if ($stdTexts.Containskey($docElementName)) {$text = $stdtexts[$docElementName]}
else {$text = $custRecord.$docElementName}
$_.Set_InnerText($text) }
}
#Save the data inserted in to the customXML doc part
$doc.save($docpart.getstream())
}
function GetDocumentPart($OfficeDocRel)
{
#get ref to word document part find all relationships that are of type CustomXML
$documentPart = $Package.GetPart([System.IO.Packaging.PackUriHelper]::ResolvePartUri($uri, $OfficeDocRel.TargetUri))
foreach ($customXmlRel in $documentPart.GetRelationshipsByType($customXmlType)) { GetCustomXmlPart($customXmlRel) }
}
function ProcessDocs([int]$docNumber)
{
#create new doc from the template
$newFile = (get-location).path + "\processedDocs\LatePayment" + $docNumber + ".docx"
$sourceFile = (get-location).path + "\Template.docx"
copy -path $sourceFile -destination $newFile
#loop through the root relationships in the doc looking for word document part - there will only be one
$Package = [System.IO.Packaging.Package]::Open($newFile)
foreach ($rel in $Package.GetRelationshipsByType($documentType)) { GetDocumentPart($rel) }
$Package.Close()
}
function Initialise()
{
if(test-path -path processedDocs) {remove-item -path Processeddocs -recurse}
$dummy = mkdir ProcessedDocs
#load customers.xml in to a global cust oject
[xml]$global:cust = get-content .\customers.xml
#load standard letter parts from standardtexts.xml in to a hash table
[xml]$stdTextsXml = get-content standardtext.xml
$stdTextsXml.texts.text | &{process{$stdTexts[$_.key] = $_.value}}
}
Initialise
## Main ##
$start = [datetime]::now
#loop through all customer childnodes in the customers.xml doc
$cust.Customers.Customer | &{begin{$records=0} process{$records++; $custRecord = $_; ProcessDocs($records)} end{$global:totRecords = $records} }
$end = [datetime]::now
"============================================================"
"Total Documents Generated: " + $totRecords
"Total processing time (seconds): " + ($end.subtract($start)).totalseconds
"Office Open XML documents generated/sec: " + $totRecords / ($end.subtract($start)).totalseconds
"============================================================"
Comments
Anonymous
June 27, 2007
PingBack from http://www.nickhodge.com/blog/archives/1990Anonymous
February 23, 2009
A year or so back I posted a link to a PowerShell Office Open XML Document Generator I wrote, see http://blogs.msdn.com/dglover/archive/2007/06/28/powershell-and-office-open-xml-format-document-generation.aspx