当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] PowerShell:将Word文档转换为PDF文档(批量)

作者:精品下载站 日期:2024-12-14 07:33:58 浏览:14 分类:玩电脑

PowerShell:将Word文档转换为PDF文档(批量)


我经常需要将 Microsoft Word 文档转换为 pdf。如果您有很多文档需要转换(假设有 1000 个或更多)怎么办?您会单独打开每个文档并单击“保存”吗?馊主意。在这篇博文中,我将为您提供一个 PowerShell 函数,使您能够使用单个命令转换多个文档。此过程称为自动化;-)。让我们深入了解一下。

目标

正如已经提到的,我们想要将多个 Microsoft Word 文档转换为 pdf。我的 cmdlet ConvertWordTo-PDF 正是这样做的。

[玩转系统] PowerShell:将Word文档转换为PDF文档(批量)

[玩转系统] PowerShell:将Word文档转换为PDF文档(批量)

凉爽的。顺便说一句,您也可以省略目标参数。然后,Pdf 文件将保存在源文件夹中。

[玩转系统] PowerShell:将Word文档转换为PDF文档(批量)

下载

单击此链接下载 PowerShell 模块。

代码

function ConvertWordTo-PDF {

<# 
 
.SYNOPSIS 

ConvertTo-PDF converts Microsoft Word documents to PDF files. 
 
.DESCRIPTION 

The cmdlet queries the given source folder including sub-folders to find *.docx and *.doc files, 
converts all found files and saves them as pdf in the Destination folder. After completition, the Destination
folder with the newly created PDF files will be opened with Windows Explorer.
 
.PARAMETER SourceFolder
 
Mandatory. Enter the source folder of your Microsoft Word documents.
 
.PARAMETER DestinationFolder

Optional. Enter the Destination folder to save the created PDF documents. If you omit this parameter, pdf files will
be saved in the Source Folder.

.EXAMPLE 

ConvertWordTo-PDF -SourceFolder C:\Temp -DestinationFolder C:\Temp1
ConvertWordTo-PDF -SourceFolder C:\temp
 
.NOTES 
Author: Patrick Gruenauer | Microsoft PowerShell MVP [2018-2021] 
Web: https://a-d.site 
 
#>

[CmdletBinding()]

param
(
 
[Parameter (Mandatory=$true,Position=0)]
[String]
$SourceFolder,
 
[Parameter (Position=1)]
[String]
$DestinationFolder = $SourceFolder

)

    $i = 0

    $word = New-Object -ComObject word.application 
    $FormatPDF = 17
    $word.visible = $false 
    $types = '*.docx','*.doc'

    If ((Test-Path $SourceFolder) -eq $false) {
    
    throw "Error. Source Folder $SourceFolder not found." } 

    If ((Test-Path $DestinationFolder) -eq $false) {
    
    throw "Error. Destination Folder $DestinationFolder not found." } 
    
    $files = Get-ChildItem -Path $SourceFolder -Include $Types -Recurse -ErrorAction Stop
    ''
    Write-Warning "Converting Files to PDF ..."
    ''
    
    foreach ($f in $files) {

        $path = $DestinationFolder + '\' + $f.Name.Substring(0,($f.Name.LastIndexOf('.')))
        $doc = $word.documents.open($f.FullName) 
        $doc.saveas($path,$FormatPDF) 
        $doc.close()
        Write-Output "$($f.Name)"
        $i++

    }
    ''
    Write-Output "$i file(s) converted."
    Start-Sleep -Seconds 2 
    Invoke-Item $DestinationFolder
    $word.Quit()
    
    
}





如何实现WordTo-PDF转换

将上面的代码复制到 PowerShell ISE (ise.exe) 或您选择的编辑器中并运行代码。然后输入命令并享受它的乐趣。

如果您想让该函数永久可用,以便每次启动 PowerShell 时该函数都可用,则必须在 C:\Program Files\WindowsPowerShell\Modules 中创建一个文件夹。将文件夹命名为 ConvertWordTo-PDF。然后将 psm1 文件复制到该文件夹中。

制作人员

感谢下面链接的作者启发了我并提出了创建该功能的想法。

https://www.der-windows-papst.de/2018/05/17/powershell-word-convert-to-pdf-doc-docx-pdf/powershell-convert-word-to-pdf/

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯