大量批次轉換舊版 Microsoft Office 格式到新版

 

問題

因為政策需求,使用者需將舊版文件轉移到新版格式。但需轉檔的文件太多,無法一一開啟轉換,需利用指令檔簡化轉換作業。




解決方式

產生舊版文件所在目錄清單供作業後比對 -> 進行批次轉換作業並將舊檔儲存於備份資料夾 -> 利用之前產生的目錄清單進行抽檢轉換後文件是否正常。

1. 產生舊版文件所在目錄清單,powershell 指令碼如下
=========================================================================
    # 設定作業資料夾 (多個資料夾時以逗號分隔,或改用Get-Content)
    $TestFolder = 'D:\'

    # 建立存放舊版文件所在目錄清單資料夾
    New-Item -Path c:\ -Name temp -ItemType Directory -Force

    # 列出舊版Word文件(.doc)所在資料夾
    Get-ChildItem -Path $TestFolder -Recurse -ErrorAction SilentlyContinue -Filter *.doc | Where-Object { $_.Extension -eq '.doc' } | select Directory -Unique | Out-File c:\temp\DocFolders.txt

    # 列出舊版Excel文件(.xls)所在資料夾
    Get-ChildItem -Path $TestFolder -Recurse -ErrorAction SilentlyContinue -Filter *.xls | Where-Object { $_.Extension -eq '.xls' } | select Directory -Unique | Out-File c:\temp\XlsFolders.txt

    # 列出舊版Powerpoint文件(.ppt)所在資料夾
    Get-ChildItem -Path $TestFolder -Recurse -ErrorAction SilentlyContinue -Filter *.ppt | Where-Object { $_.Extension -eq '.ppt' } | select Directory -Unique | Out-File c:\temp\PptFolders.txt

    =========================================================================

    2. 利用 powershell 指令碼將所有文件轉換到新版(.docx)
    =========================================================================
    # 設定作業資料夾 (多個資料夾時以逗號分隔,或改用Get-Content)
    $folderpath = 'D:\'
    $filetype ="*doc"

    Add-Type -AssemblyName Microsoft.Office.Interop.Word

    $docFixedFormat = [Microsoft.Office.Interop.Word.WdSaveFormat]::wdFormatDocumentDefault
    write-host $docFixedFormat
    $word = New-Object -ComObject word.application
    $word.visible = $true
    Get-ChildItem -Path $folderpath -Include $filetype -Recurse | 
    ForEach-Object `
    {
    $path = ($_.fullname).substring(0, ($_.FullName).lastindexOf("."))
    "Converting $path"
    $Documents = $word.Documents.Open($_.fullname)

    $path += ".docx"
    $Documents.saveas($path, $docFixedFormat)
    $Documents.close()
    # 在舊格式文件所在資料夾建立備份資料夾保留原本的舊版文件
            $oldFolder = $path.substring(0, $path.lastIndexOf("\")) + "\OldVersionBackup"
    write-host $oldFolder
    if(-not (test-path $oldFolder))
    {
    new-item $oldFolder -type directory
    }
    move-item $_.fullname $oldFolder
    }
    $word.Quit()
    $word = $null
    [gc]::collect()
    [gc]::WaitForPendingFinalizers()
    =========================================================================

    3. 利用 powershell 指令碼將所有文件轉換到新版(.xlsx)
    =========================================================================
    # 設定作業資料夾 (多個資料夾時以逗號分隔,或改用Get-Content)
    $folderpath = 'D:\'
    $filetype ="*xls"

    Add-Type -AssemblyName Microsoft.Office.Interop.Excel

    $xlFixedFormat = [Microsoft.Office.Interop.Excel.XlFileFormat]::xlWorkbookDefault
    write-host $xlFixedFormat
    $excel = New-Object -ComObject excel.application
    $excel.visible = $true
    Get-ChildItem -Path $folderpath -Include $filetype -recurse | 
    ForEach-Object `
    {
    $path = ($_.fullname).substring(0, ($_.FullName).lastindexOf("."))
    "Converting $path"
    $workbook = $excel.workbooks.open($_.fullname)

    $path += ".xlsx"
    $workbook.saveas($path, $xlFixedFormat)
    $workbook.close()
            # 在舊格式文件所在資料夾建立備份資料夾保留原本的舊版文件
    $oldFolder = $path.substring(0, $path.lastIndexOf("\")) + "\OldVersionBackup"
    write-host $oldFolder
    if(-not (test-path $oldFolder))
    {
    new-item $oldFolder -type directory
    }
    move-item $_.fullname $oldFolder
    }
    $excel.Quit()
    $excel = $null
    [gc]::collect()
    [gc]::WaitForPendingFinalizers()
    =========================================================================

    4. 利用 powershell 指令碼將所有文件轉換到新版(.pptx)
    =========================================================================
    # 設定作業資料夾 (多個資料夾時以逗號分隔,或改用Get-Content)
    $folderpath = 'D:\'
    $filetype ="*ppt"

    Add-Type -AssemblyName Microsoft.Office.Interop.PowerPoint

    $PptFixedFormat = [Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType]::ppSaveAsOpenXMLPresentation
    write-host $PptFixedFormat
    $PowerPoint = New-Object -ComObject PowerPoint.application
    $PowerPoint.visible = $true
    Get-ChildItem -Path $folderpath -Include $filetype -Recurse | 
    ForEach-Object `
    {
    $path = ($_.fullname).substring(0, ($_.FullName).lastindexOf("."))
    "Converting $path"
    $Presentation = $PowerPoint.Presentations.Open($_.fullname)

    $path += ".pptx"
    $Presentation.saveas($path, $PptFixedFormat)
    $Presentation.close()
            # 在舊格式文件所在資料夾建立備份資料夾保留原本的舊版文件
    $oldFolder = $path.substring(0, $path.lastIndexOf("\")) + "\OldVersionBackup"
    write-host $oldFolder
    if(-not (test-path $oldFolder))
    {
    new-item $oldFolder -type directory
    }
    move-item $_.fullname $oldFolder
    }
    $PowerPoint.Quit()
    $PowerPoint = $null
    [gc]::collect()
    [gc]::WaitForPendingFinalizers()
    =========================================================================

    5. 依之前產生的各舊版文件所在目錄清單進行轉換後檢查。



    心得與觀察

    基本上轉換格式指令碼的作用只是將原本的文件開啟並另存為新版格式,其中因版本差異而導致的格式或涵式異常都還是需要人工介入修改的。


    留言

    熱門文章