关于Windows:Visio到图像命令行转换

Visio to image command line conversion

在工作中,我们大量使用Visio绘图作为文档支持。不幸的是,vsd文件在我们的wiki或文档提取工具(如javadoc,doxygen或naturaldocs)中无法很好地发挥作用。虽然可以将Visio文件手动转换为图像,但是将图像保持最新状态很麻烦,并且图像文件注定会过时。让我们面对现实:在版本控制中生成文件感觉太错误了。

因此,我正在寻找一种命令行工具,该工具可以将vsd文件转换为jpeg,png,gif或任何可以转换为浏览器可以显示的图像的图像。最好在Unix下运行,但仅Windows也可以。我可以处理自动化链的其余部分,cron作业,图像到图像的转换以及ssh,scp,多个文件等。

这就是为什么我转向您:我找不到这样的工具。我认为我什至不能为此工具付费。我的Google-fu是否已完全关闭?你能帮我吗?

我的意思是,这一定有可能。必须有一种方法可以通过COM连接到Visio,并将其保存为图像。我正在使用Visio2007。

预先感谢。


我使用VB6快速拍打了一些东西,可以从以下位置下载它:
http://fournier.jonathan.googlepages.com/Vis2Img.exe

您只需传递输入visio文件路径,然后传递输出文件路径(基于文件扩展名的visio导出),以及可选的要导出的页码。

这也是我使用的源代码,如果您想弄乱它或将其变成VBScript之类的东西,它应该可以工作,尽管您需要完成将其转换为后期绑定代码的过程。

希望有帮助,

乔恩

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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


F#2.0脚本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//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


您可以尝试" Visio到图像"转换器

http://soft.postpdm.com/visio2image.html

通过MS Visio 2007和2010测试


There has to be a way to hook into Visio with COM and get it to save as image.

如果您知道如何使用COM东西,为什么不尝试自己写东西呢?毕竟,如果您找不到已经做过的任何事情,并且知道自己可以弄清楚该怎么做,为什么不自己写点什么呢?

编辑:详细说明我的评论:在这种情况下,编写某种脚本似乎是您的最佳选择,并且使用comtypes库,至少Python对此非常有用。可以在这里找到:http://starship.python.net/crew/theller/comtypes/当然,正如我所说,如果您喜欢使用其他脚本语言,则可以尝试使用该脚本语言。事实是,在这一点上,我只真正将COM与VBA和Python一起使用(顺便说一句,我相信Microsoft如今倾向于指的是"自动化",而不是专门指的是COM。)关于Python的好处是,它是一种解释语言,因此您只需要针对所使用的不同操作系统的解释程序版本,以及Windows,OSX,Linux,Unix等版本。另一方面,我怀疑您是否可以在非操作系统上使用COM -Windows系统没有任何黑客手段,因此您很可能必须直接解析源文件中的数据(即使Visio的默认格式似乎使用某种形式的XML,也可能是Microsoft似乎喜欢的专有格式之一) )。

如果您以前从未使用过Python,那么Python文档中有一个很好的入门指南:http://docs.python.org/3.1/tutorial/index.html

当然,您还需要Python解释器本身:http://python.org/download/releases/3.1/(请注意,安装后可能必须手动将Python目录添加到PATH环境变量中。)

编写脚本时,运行脚本的语法可能类似于" python visioexport.py <source/original file[ with path]>[ <new file[ with path]>]"(假设脚本文件位于Python目录中),而新文件默认为相同的文件名称和原始文件夹位于同一文件夹/目录中(尽管扩展名不同;实际上,如果您愿意,可以将其设置为导出为多种格式,默认格式为您选择的任何默认扩展名由另一扩展名指定,您可以在文件名中指定一个。同样,您也可以对其进行设置,以便如果在源文件之后仅拥有新文件名,而未指定路径,则它将与该新文件一起保存当然,如果您没有指定源文件的路径,只需指定一个文件名,就可以将其设置为从当前目录中获取文件。

关于文件格式的问题:在我看来,转换为SVG可能是最好的选择,因为这样做可以节省空间,并且可以更好地将原始图像的状态反映为矢量图像。另一方面,从Visio格式到SVG的转换并不完美(或者,至少在Visio 2003中不是这样;我找不到类似于Visio 2007的信息源),并且如图所示,您可能必须修改生成的XML文件(尽管可以在导出文件后通过Python标准库的一部分使用脚本来完成此操作)。如果您不介意位图的其他文件大小,而宁愿不必包含用于修复生成的SVG文件的其他代码,那么您可能应该只使用位图格式,例如PNG。