簡體   English   中英

如何使用python將Microsoft Doc轉換為PDF文件

[英]How to convert Microsoft Doc into PDF file using python

我正在嘗試將 Microsoft Docx 轉換為 PDF 文件並使用此 python 腳本

import os
import win32com. client
#pip install pywin32

wdFormatPDF = 17
in_file = "in_file/path/Input.docx"
out_file = "out_file/path/output.pdf"

word = win32com.client.Dispatch('Word.Application')
doc = word.Documents.Open(in_file)
doc.SaveAs(out_file, FileFormat = wdFormatPDF)
doc.Close()
word.Quit()``

But I got this error

```Encoding, InsertLineBreaks, AllowSubstitutions, LineEnding, AddBiDiMarks
pywintypes.com_error: (-214738676867, 'Exception occurred.', (0, 'Microsoft Word', 'Command failed', 'C:\\Program Files\\Microsoft Office\\Office12\\1033\\WDMAIN11.CHM', 587257, -214687359843090), None)`

And I also tried another plugin "from docx2pdf import convert" But the same error is coming. So plz suggest how to solve my problem.

嘗試這個:

import win32com.client as win32
from os import path
word = win32.DispatchEx("Word.Application")
in_file = path.abspath('c:\\sample.docx')
out_file = path.abspath('c:\\sample.pdf')
# just one argument here
doc = word.Documents.Open(in_file)
# was: 'new_file'
doc.SaveAs(out_file, FileFormat=17)
doc.Close()
word.Quit()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM