You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何批量将docx文件转换为PDF?mailmerge生成文档转档需求

Hey there! Totally get your pain—dealing with a mountain of mailmerge-generated Word docs and needing to turn them all into PDFs sounds like a total slog. Let’s break down a few solid, no-fuss ways to batch convert those .docx files to PDF, depending on what tools you’re comfortable with.

方法1:Word宏(适合Office常客)

If you’re already using Word for your mailmerges, this is the most straightforward option—no extra software needed. Here’s how to set it up:

  1. Open Microsoft Word, go to the Developer tab (if you don’t see it, enable it via File > Options > Customize Ribbon).
  2. Click Macros, name your macro something like BatchConvertToPDF, then click Create.
  3. Paste this VBA code into the editor, replacing the folder paths with your actual input/output directories:
Sub BatchConvertToPDF()
    Dim inputFolder As String
    Dim outputFolder As String
    Dim docFile As String
    Dim doc As Document
    
    ' Update these paths to match your folders!
    inputFolder = "C:\Path\To\Your\Word\Docs\"
    outputFolder = "C:\Path\To\Save\PDFs\"
    
    ' Create output folder if it doesn't exist
    If Dir(outputFolder, vbDirectory) = "" Then
        MkDir outputFolder
    End If
    
    ' Loop through all .docx files in the input folder
    docFile = Dir(inputFolder & "*.docx")
    Do While docFile <> ""
        Set doc = Documents.Open(inputFolder & docFile)
        ' Export as PDF with the same filename
        doc.ExportAsFixedFormat _
            OutputFileName:=outputFolder & Replace(docFile, ".docx", ".pdf"), _
            ExportFormat:=wdExportFormatPDF
        doc.Close SaveChanges:=wdDoNotSaveChanges
        docFile = Dir()
    Loop
    
    MsgBox "Batch conversion complete! 🎉"
End Sub
  1. Save the macro, then run it by going back to the Developer tab > Macros > selecting your macro > Run.

Pro tip: Make sure to enable macros when prompted (Word disables them by default for security—just trust your own code!).

方法2:Python脚本(适合代码-savvy users)

If you’re comfortable with a little code, this method gives you more flexibility (like adding custom filename rules or logging). Here’s what you need to do:

  1. First, install the docx2pdf library (it uses your local Word installation to handle the conversion):
pip install docx2pdf
  1. Create a new Python file (e.g., convert_docs.py) and paste this code, updating the folder paths:
from docx2pdf import convert
import os

# Set your input and output folders
input_dir = r"C:\Path\To\Your\Word\Docs"
output_dir = r"C:\Path\To\Save\PDFs"

# Create output folder if it doesn't exist
os.makedirs(output_dir, exist_ok=True)

# Loop through all .docx files and convert
for filename in os.listdir(input_dir):
    if filename.lower().endswith(".docx"):
        doc_path = os.path.join(input_dir, filename)
        pdf_path = os.path.join(output_dir, filename.replace(".docx", ".pdf"))
        convert(doc_path, pdf_path)

print("All files converted successfully!")
  1. Run the script from your terminal/command prompt:
python convert_docs.py

Note: This requires Microsoft Word to be installed on your computer—docx2pdf just automates the Word interface in the background.

方法3:PowerShell脚本(适合Windows command-line fans)

No need for Python or macros—Windows’ built-in PowerShell can handle this too. Here’s the script:

  1. Open PowerShell (search for it in the Start menu).
  2. Paste this code, replacing the folder paths:
$inputFolder = "C:\Path\To\Your\Word\Docs"
$outputFolder = "C:\Path\To\Save\PDFs"

# Create output folder if missing
if (-not (Test-Path -Path $outputFolder)) {
    New-Item -ItemType Directory -Path $outputFolder | Out-Null
}

# Initialize Word object (run in background)
$word = New-Object -ComObject Word.Application
$word.Visible = $false

# Convert each .docx file
Get-ChildItem -Path $inputFolder -Filter *.docx | ForEach-Object {
    $doc = $word.Documents.Open($_.FullName)
    $pdfPath = Join-Path -Path $outputFolder -ChildPath ($_.BaseName + ".pdf")
    $doc.ExportAsFixedFormat($pdfPath, 17)  # 17 = wdExportFormatPDF
    $doc.Close()
}

# Clean up Word process
$word.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($word) | Out-Null

Write-Host "Batch conversion done!"
  1. Press Enter to run it.

Pro tip: If you run into permission issues, right-click PowerShell and select "Run as administrator" before pasting the code.

Pick the method that fits your workflow best—all of them will get the job done without manually opening each Word doc!

内容的提问来源于stack exchange,提问作者Mohamed Elgazar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 14:27:56