Last active
May 15, 2024 00:18
-
-
Save ap0llo/1e24d9e66ef58f5de064c8aba2198b9e to your computer and use it in GitHub Desktop.
Powershell script that exports Visio drawings in the current directory to png
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Powershell script that exports visio drawings to png | |
# Based on a F# script with the same purpose which can be found at | |
# http://stackoverflow.com/questions/1145269/visio-to-image-command-line-conversion | |
$outputFormat = ".png" | |
$inputFilePattern = "*.vsdx" | |
# Load Visio Interop Assembly | |
[Reflection.Assembly]::LoadWithPartialname("Microsoft.Office.Interop.Visio") > $null | |
# Visio Settings | |
$visOpenRO = 2 | |
$visOpenMinimized = 16 | |
$visOpenHidden = 64 | |
$visOpenMacrosDisabled = 128 | |
$visOpenNoWorkspace = 256 | |
function Export-VisioDrawing($file) | |
{ | |
$flags = [System.Convert]::ToInt16($visOpenRO + $visOpenMinimized + $visOpenHidden + $visOpenMacrosDisabled + $visOpenNoWorkspace) | |
$application = New-Object "Microsoft.Office.Interop.Visio.ApplicationClass" | |
$document = $application.Documents.OpenEx($file.FullName, $flags) | |
foreach($page in $document.Pages) | |
{ | |
$fileNameWithoutExtension = [System.IO.Path]::GetFileNameWithoutExtension($file.Name) | |
# if document contains multiple pages, append page name to output file name | |
# otherwise use the same name as the input file | |
if($document.Pages.Count -gt 1) | |
{ | |
$imagePath = Join-Path $file.Directory.FullName "$($fileNameWithoutExtension)_$($page.Name)$($outputFormat)" | |
} | |
else | |
{ | |
$imagePath = Join-Path $file.Directory.FullName "$($fileNameWithoutExtension)$($outputFormat)" | |
} | |
$page.Export($imagePath) | |
} | |
$document.Close() | |
$application.Quit() | |
} | |
# Get visio files in the current directory and export all of them | |
$files = Get-ChildItem $inputFilePattern | |
foreach($file in $files) | |
{ | |
Export-VisioDrawing $file | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Worked a charm! Thanks for saving me a lot of work!