Làm thế nào để xuất tài liệu sang PDF trong Python

Làm thế nào để xuất tài liệu sang PDF trong Python

Aspose.Words FOSS for Python exports loaded Word documents to PDF through a built-in rendering pipeline. The library reads DOCX, DOC, RTF, TXT, and Markdown as input formats; PDF is an output-only format.


PDF xuất khẩu

PDF là một định dạng xuất khẩu duy nhất. lưu tài liệu vào PDF bằng cách sử dụng Document.save():

import aspose.words_foss as aw

doc = aw.Document("report.docx")
doc.save("report.pdf")

Định dạng xuất phát được xác định bởi phần mở rộng tệp. PDF tác giả giữ định dạng văn bản, bảng, hình ảnh và bố trí trang từ tài liệu nguồn.


Sử dụng PdfSaveOptions

Để kiểm soát tốt hơn về xuất PDF, hãy chuyển qua một PdfSaveOptions trường hợp :

import aspose.words_foss as aw
from aspose.words_foss.saving import PdfSaveOptions

doc = aw.Document("input.docx")
opts = PdfSaveOptions()
doc.save("output.pdf", opts)

Batch chuyển đổi

Chuyển đổi nhiều tệp thành một vòng tròn:

import aspose.words_foss as aw
from pathlib import Path

for src in Path("docs/").glob("*.docx"):
    doc = aw.Document(str(src))
    doc.save(str(src.with_suffix(".pdf")))
    print(f"Converted {src.name}")

Xem thêm

 Tiếng Việt