You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

使用python-docx保存修改后的docx文件转PDF后Adobe显示错误文件名的技术求助

Fix: Adobe Acrobat Displays Wrong Filename After Converting python-docx-Saved docx to PDF

I’ve run into this exact issue before—python-docx doesn’t update the document’s internal metadata when you save a modified existing file, and Adobe Acrobat prioritizes that metadata over the actual filename when displaying the document title. Here’s how to fix it:

Why This Happens

When you open an existing docx with docx.Document(), the library preserves all the original document’s core metadata—including the Title property. If your source document (20210501.docx) was originally a new, unsaved Word document (which defaults to names like Dokument1), that title metadata sticks around even after you save it as test.docx. When you convert to PDF via Word’s COM object, it carries over that original title metadata to the PDF, so Adobe shows Microsoft Word - Dokument1 instead of your filename.

Manual saves work because Word automatically updates the title metadata to match the filename when you save through the UI, and your test with a manually created docx works because that document’s title metadata is already set correctly.

The Fix: Update the Document’s Core Title Property

Before saving your modified docx, explicitly set its core title property to match your desired filename. Here’s your revised code:

import os
import comtypes.client
from docx import Document
from docx.opc.coreprops import CoreProperties

def word_to_pdf(_in, _out):
    pdf_format_key = 17
    file_in = os.path.abspath(_in)
    file_out = os.path.abspath(_out)
    worddoc = comtypes.client.CreateObject("Word.Application")
    doctemp = worddoc.Documents.Open(file_in)
    doctemp.SaveAs(file_out, FileFormat=pdf_format_key)
    doctemp.Close()  # Fixed: COM methods use camelCase, not lowercase
    worddoc.Quit()

# Open the existing document
doc = Document("C:/Users/myname/Desktop/20210501.docx")

# Update the core title metadata to match your filename
doc.core_properties.title = "test"

# Save the modified document
doc.save("test.docx")

# Convert to PDF
word_to_pdf("test.docx", "test.pdf")

Key Notes:

  • Metadata Update: By setting doc.core_properties.title = "test", you ensure the document’s internal title matches the filename. When Word converts it to PDF, this title is carried over to the PDF’s metadata, so Adobe displays it correctly.
  • Fix for Word COM Method: I corrected doctemp.close() to doctemp.Close()—COM objects for Word use camelCase method names, and the lowercase version would throw an error, leaving Word processes running in the background.

This should resolve the filename mismatch in Adobe Acrobat while keeping your existing workflow intact.

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

火山引擎 最新活动