windows Visio 到图像命令行转换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1145269/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Visio to image command line conversion
提问by Peter Monsson
At work we make pretty extensive use of Visio drawing as support for documentation. Unfortunately vsd files don't play nicely with our wiki or documentation extraction tools like javadoc, doxygen or naturaldocs. While it is possible to convert Visio files to images manually, it's just a hassle to keep the image current and the image files are bound to get out of date. And let's face it: Having generated files in revision control feels so wrong.
在工作中,我们非常广泛地使用 Visio 绘图作为文档支持。不幸的是,vsd 文件不能很好地与我们的 wiki 或文档提取工具(如 javadoc、doxygen 或 naturaldocs)配合使用。虽然可以手动将 Visio 文件转换为图像,但保持图像最新很麻烦,而且图像文件肯定会过时。让我们面对现实:在版本控制中生成文件感觉非常错误。
So I'm looking for a command line tool that can convert a vsd file to jpeg, png, gif or any image that can be converted to an image that a browser can display. Preferably it will run under unix, but windows only is also fine. I can handle the rest of the automation chain, cron job, image to image conversion and ssh, scp, multiple files, etc.
所以我正在寻找一个命令行工具,可以将 vsd 文件转换为 jpeg、png、gif 或任何可以转换为浏览器可以显示的图像的图像。最好是在 unix 下运行,但只有 windows 也可以。我可以处理自动化链的其余部分、cron 作业、图像到图像的转换以及 ssh、scp、多个文件等。
And that's why I'm turning to you: I can't find such a tool. I don't think I can even pay for such a tool. Is my Google-fu completely off? Can you help me?
这就是我求助于您的原因:我找不到这样的工具。我认为我什至无法为这样的工具付费。我的 Google-fu 完全关闭了吗?你能帮助我吗?
I mean, it has got to be possible. There has to be a way to hook into Visio with COM and get it to save as image. I'm using Visio 2007 by the way.
我的意思是,它必须是可能的。必须有一种方法可以使用 COM 连接到 Visio 并将其另存为图像。顺便说一下,我正在使用 Visio 2007。
Thanks in advance.
提前致谢。
采纳答案by Jon Fournier
I slapped something together quickly using VB6, and you can download it at: http://fournier.jonathan.googlepages.com/Vis2Img.exe
我使用 VB6 快速拼凑了一些东西,您可以在以下网址下载:http: //fournier.jonathan.googlepages.com/Vis2Img.exe
You just pass in the input visio file path, then the output file path (visio exports based on file extension) and optionally the page number to export.
您只需传入输入 visio 文件路径,然后是输出文件路径(基于文件扩展名的 visio 导出)以及要导出的页码(可选)。
Also here is the source code I used, if you want to mess with it or turn it into a VBScript or something, it should work, though you'd need to finish converting it to late-bound code.
这里也是我使用的源代码,如果你想弄乱它或把它变成一个 VBScript 或其他东西,它应该可以工作,尽管你需要完成将它转换为后期绑定的代码。
hope that helps,
希望有帮助,
Jon
乔恩
Dim TheCmd As String
Const visOpenRO = 2
Const visOpenMinimized = 16
Const visOpenHidden = 64
Const visOpenMacrosDisabled = 128
Const visOpenNoWorkspace = 256
Sub Main()
' interpret command line arguments - separated by spaces outside of double quotes
TheCmd = Command
Dim TheCmds() As String
If SplitCommandArg(TheCmds) Then
If UBound(TheCmds) > 1 Then
Dim PageNum As Long
If UBound(TheCmds) >= 3 Then
PageNum = Val(TheCmds(3))
Else
PageNum = 1
End If
' if the input or output file doesn't contain a file path, then assume the same
If InStr(1, TheCmds(1), "\") = 0 Then
TheCmds(1) = App.Path & "\" & TheCmds(1)
End If
If InStr(1, TheCmds(2), "\") = 0 Then
TheCmds(2) = App.Path & "\" & TheCmds(2)
End If
ConvertVisToImg TheCmds(1), TheCmds(2), PageNum
Else
' no good - need an in and out file
End If
End If
End Sub
Function ConvertVisToImg(ByVal InVisPath As String, ByVal OutImgPath As String, PageNum As Long) As Boolean
ConvertVisToImg = True
On Error GoTo PROC_ERR
' create a new visio instance
Dim VisApp As Visio.Application
Set VisApp = CreateObject("Visio.Application")
' open invispath
Dim ConvDoc As Visio.Document
Set ConvDoc = VisApp.Documents.OpenEx(InVisPath, visOpenRO + visOpenMinimized + visOpenHidden + visOpenMacrosDisabled + visOpenNoWorkspace)
' export to outimgpath
If Not ConvDoc.Pages(PageNum) Is Nothing Then
ConvDoc.Pages(PageNum).Export OutImgPath
Else
MsgBox "Invalid export page"
ConvertVisToImg = False
GoTo PROC_END
End If
' close it off
PROC_END:
On Error Resume Next
VisApp.Quit
Set VisApp = Nothing
Exit Function
PROC_ERR:
MsgBox Err.Description & vbCr & "Num:" & Err.Number
GoTo PROC_END
End Function
Function SplitCommandArg(ByRef Commands() As String) As Boolean
SplitCommandArg = True
'read through command and break it into an array delimited by space characters only when we're not inside double quotes
Dim InDblQts As Boolean
Dim CmdToSplit As String
CmdToSplit = TheCmd 'for debugging command line parser
'CmdToSplit = Command
Dim CharIdx As Integer
ReDim Commands(1 To 1)
For CharIdx = 1 To Len(CmdToSplit)
Dim CurrChar As String
CurrChar = Mid(CmdToSplit, CharIdx, 1)
If CurrChar = " " And Not InDblQts Then
'add another element to the commands array if InDblQts is false
If Commands(UBound(Commands)) <> "" Then ReDim Preserve Commands(LBound(Commands) To UBound(Commands) + 1)
ElseIf CurrChar = Chr(34) Then
'set InDblQts = true
If Not InDblQts Then InDblQts = True Else InDblQts = False
Else
Commands(UBound(Commands)) = Commands(UBound(Commands)) & CurrChar
End If
Next CharIdx
End Function
回答by Dzmitry Lahoda
F# 2.0 script:
F# 2.0 脚本:
//Description: // Generates images for all Visio diagrams in folder were run according to pages names //Tools: // Visio 2010 32bit is needed to open diagrams (I also installed VisioSDK32bit.exe on my Windows 7 64bit) #r "C:/Program Files (x86)/Microsoft Visual Studio 10.0/Visual Studio Tools for Office/PIA/Office14/Microsoft.Office.Interop.Visio.dll" open System open System.IO open Microsoft.Office.Interop.Visio let visOpenRO = 2 let visOpenMinimized = 16 let visOpenHidden = 64 let visOpenMacrosDisabled = 128 let visOpenNoWorkspace = 256 let baseDir = Environment.CurrentDirectory; let getAllDiagramFiles = Directory.GetFiles(baseDir,"*.vsd") let drawImage fullPathToDiagramFile = let diagrammingApplication = new ApplicationClass() let flags = Convert.ToInt16(visOpenRO + visOpenMinimized + visOpenHidden + visOpenMacrosDisabled + visOpenNoWorkspace) let document = diagrammingApplication.Documents.OpenEx(fullPathToDiagramFile,flags) for page in document.Pages do let imagePath = Path.Combine(baseDir, page.Name + ".png") page.Export (imagePath) document.Close() diagrammingApplication.Quit() let doItAll = Array.iter drawImage getAllDiagramFiles doItAll
回答by Y.N
You can try "Visio to image" converter
您可以尝试“Visio 到图像”转换器
http://soft.postpdm.com/visio2image.html
http://soft.postpdm.com/visio2image.html
Tested with MS Visio 2007 and 2010
使用 MS Visio 2007 和 2010 进行测试
回答by JAB
There has to be a way to hook into Visio with COM and get it to save as image.
必须有一种方法可以使用 COM 连接到 Visio 并将其另存为图像。
Why not try writing something yourself, then, if you know how to use COM stuff? After all, if you can't find anything already made to do it, and you know you can figure out how to do it yourself, why not write something to do it yourself?
如果您知道如何使用 COM 的东西,为什么不尝试自己编写一些东西呢?毕竟,如果你找不到任何已经做过的事情,而且你知道你可以自己弄清楚如何去做,为什么不自己写一些东西来做呢?
EDIT: Elaborating a bit on what I stated in my comment: writing a script of some sort does seem to be your best option in this situation, and Python, at least, would be quite useful for that, using the comtypes library found here: http://starship.python.net/crew/theller/comtypes/Of course, as I said, if you prefer to use a different scripting language, then you could try using that; the thing is, I've only really used COM with VBA and Python at this point (As an aside, Microsoft tends to refer to "Automation" these days rather than specifically referencing COM, I believe.) The nice thing about Python is that it's an interpreted language, and thus you just need a version of the interpreter for the different OSes you're using, with versions for Windows, OSX, Linux, Unix, etc. On the other hand, I doubt you can use COM on non-Windows systems without some sort of hack, so you may very well have to parse the data in the source files directly (and even though Visio's default formats appear to use some form of XML, it's probably one of those proprietary formats Microsoft seems to love).
编辑:详细说明我在评论中所说的内容:在这种情况下,编写某种脚本似乎是您的最佳选择,而 Python 至少对此非常有用,使用这里的 comtypes 库:http://starship.python.net/crew/theller/comtypes/当然,正如我所说,如果您更喜欢使用不同的脚本语言,那么您可以尝试使用它;问题是,在这一点上,我只真正将 COM 与 VBA 和 Python 一起使用(顺便说一句,现在微软倾向于指代“自动化”,而不是专门指代 COM,我相信。)Python 的好处在于它是一种解释性语言,因此您只需要一个适用于您使用的不同操作系统的解释器版本,包括适用于 Windows、OSX、Linux、Unix 等的版本。另一方面,我怀疑您是否可以在非操作系统上使用 COM -Windows 系统没有某种 hack,因此您可能必须直接解析源文件中的数据(尽管 Visio 的默认格式似乎使用某种形式的 XML,但它可能是 Microsoft 似乎喜欢的专有格式之一)。
If you haven't used Python before, the Python documentation has a good tutorial to get people started: http://docs.python.org/3.1/tutorial/index.html
如果您之前没有使用过 Python,Python 文档有一个很好的入门教程:http: //docs.python.org/3.1/tutorial/index.html
And, of course, you'll want the Python interpreter itself: http://python.org/download/releases/3.1/(Note that you may have to manually add the Python directory to the PATH environment variable after installation.)
而且,当然,您需要 Python 解释器本身:http: //python.org/download/releases/3.1/(请注意,您可能需要在安装后手动将 Python 目录添加到 PATH 环境变量中。)
When you write the script, you could probably have the syntax for running the script be something like "python visioexport.py <source/original file[ with path]>[ <new file[ with path]>]
" (assuming the script file is in your Python directory), with the new file defaulting to a file of the same name and in the same folder/directory as the original (albeit with a different extension; in fact, if you wish, you could set it up to export to multiple formats, with the format defaulting to that of whatever default extension you choose and being specified by an alternate extension of you specify one in the file name. As well, you could likely set it up so that if you only have the new file name after the source file, no path specified, it'll save with that new file name to the source file's directory. And, of course, if you don't specify a path for the source file, just a file name, you could set it up to get the file from the current directory).
编写脚本时,运行脚本的语法可能类似于“python visioexport.py <source/original file[ with path]>[ <new file[ with path]>]
"(假设脚本文件在您的 Python 目录中),新文件默认为与原始文件同名且位于同一文件夹/目录中的文件(尽管具有不同的扩展名;事实上,如果您愿意,您可以可以将其设置为导出为多种格式,格式默认为您选择的任何默认扩展名的格式,并由您在文件名中指定一个的备用扩展名指定。同样,您可能会设置它以便如果你只有源文件后面的新文件名,没有指定路径,它会用这个新文件名保存到源文件的目录中。当然,如果你没有为源文件指定路径,只需一个文件名,您可以将其设置为从当前目录中获取文件)。
On the topic of file formats: it seems to me that converting to SVG might be the best thing to do, as it would be more space-efficient and would better reflect the original images' status as vectored images. On the other hand, the conversion from a Visio format to SVG is not perfect (or, at least, it wasn't in Visio 2003; I can't find a source of info similar to this onefor Visio 2007), and as seen here, you may have to modify the resultant XML file (though that could be done using the script, after the file is exported, via parts of the Python standard library). If you don't mind the additional file size of bitmaps, and you'd rather not have to include additional code to fix resultant SVG files, then you probably should just go with a bitmap format such as PNG.
关于文件格式的话题:在我看来,转换为 SVG 可能是最好的做法,因为它会更节省空间,并且可以更好地反映原始图像作为矢量图像的状态。在另一方面,从Visio格式为SVG转换是不完美的(或者,至少,它不是在Visio 2003,我无法找到信息的类似源这一块的Visio 2007),并作为在这里看到,您可能必须修改生成的 XML 文件(尽管可以使用脚本完成,在文件导出后,通过 Python 标准库的一部分)。如果您不介意位图的额外文件大小,并且您不想包含额外的代码来修复生成的 SVG 文件,那么您可能应该只使用位图格式,例如 PNG。