使用 Python API 進行文檔轉換

使用 Aspose.Words for Python 通過 .NET 轉換 Microsoft® Office Word、PDF、圖像和各種其他格式。

 

Total Python API 加快了從頭開始開發文檔自動化解決方案或增強現有應用程序以創建、編輯或轉換文檔、演示文稿、電子郵件和 3D 文件的速度。 Python API 不僅可以處理 Microsoft Office Word 和演示文稿幻燈片,還可以處理 PDF、HTML、圖像和電子郵件文件等等。 API 不依賴於任何軟件,是一整套文檔管理和操作解決方案。

將 Microsoft Word 轉換為 PDF

Total Python API 支持多種格式的轉換,例如 Microsoft Word 到 PDF、圖像、Markdown 和 HTML。 API 使將 Word 文檔轉換為 PDF 的過程變得簡單,其輸出質量與 DOC、DOCX 文件一樣接近文檔。 過程是將 DOC 或 DOCX 文件加載到 Document 對像中,然後調用 save 方法,帶有目標 PDF 格式及其目錄路徑。 就是這麼簡單。如果需要指定 PDF 標準,如 PDF 1.7 或 1.5,API 提供 PdfComplaence 枚舉,用於設置 PdfSaveOptions()

Python - Word 到 PDF 的轉換

import aspose.words as aw
doc = aw.Document(directorypath + "wordfile.docx")
saveOptions = aw.saving.PdfSaveOptions()
saveOptions.compliance = aw.saving.PdfCompliance.PDF17
doc.save(directorypath + "WorkingWithPdfSaveOptions.conversion_to_pdf_17.pdf", saveOptions)

Microsoft Word 到圖像的轉換

文字到圖片的轉換是 Python API 的另一個功能。 除了轉換之外,還可以輕鬆設置各種保存選項,例如亮度、對比度、水平和垂直分辨率等。過程是,通過 Document 對象加載文檔,然後使用具有指定路徑的所需圖像文件擴展名調用 save 方法。 為了指定各種保存選項,API 提供了 ImageSaveOptionsFixedPageSaveOptionsSaveOptions 類可以從所需的場景開始使用。 下面的代碼示例演示了通過應用一些附加設置來創建第一個文檔頁面的預覽。

Python - 文字到圖像的轉換

import aspose.words as aw
doc = aw.Document(dirPath + "Rendering.docx")
options = aw.saving.ImageSaveOptions(aw.SaveFormat.JPEG)
options.page_set = aw.saving.PageSet(0)
options.image_brightness = 0.3
options.image_contrast = 0.7
options.horizontal_resolution = 72
doc.save(artifacts_dirPath + "WorkingWithImageSaveOptions.get_jpeg_page_range.jpeg", options)

將 Microsoft PowerPoint 轉換為 Word

Python API 支持將 Microsoft PowerPoint PPT / PPTX 轉換為 Word DOC / DOCX 文件。 兩個 API Aspose.Slides for Python via .NETAspose.Words for Python via .NET 用於執行此轉換。 使用 Presentation 加載 PPT / PPTX 文件。 獲取 Words Document 類對象。遍歷每張幻燈片,生成並插入幻燈片圖像,然後通過遍歷幻燈片形狀來插入幻燈片文本。

Python - PowerPoint 幻燈片到 Word 轉換

import aspose.slides as slides
import aspose.words as words
presentation = slides.Presentation("pres.pptx")
doc = words.Document()
builder = words.DocumentBuilder(doc)
for index in range(presentation.slides.length):
slide = presentation.slides[index]
slide.get_thumbnail(2,2).save("slide_{i}.png".format(i = index), drawing.imaging.ImageFormat.png)
builder.insert_image("slide_{i}.png".format(i = index))
for shape in slide.shapes:
if (type(shape) is slides.AutoShape):
builder.writeln(shape.text_frame.text)
builder.insert_break(words.BreakType.PAGE_BREAK)
doc.save("presentation.docx")
 

電子郵件到 Word、PDF、HTML 和圖像的轉換

對於轉換為 PDF、Word、圖像和 HTML 的電子郵件文件,Email Python API Aspose.Email for Python via .NET 執行轉換。 API 將源文件加載到其對像模型中,並使用相關參數調用 Save 方法。

Python - 將電子郵件文件轉換為 Word

import aspose.words as aw
msg= MailMessage.load(dir + "msgtemplate.msg")
msg.save("htmloutput.html", SaveOptions.default_html)
doc = aw.Document("htmloutput.html")
doc.save("emailtoword.docx")