教你用8行代碼將word轉換爲pdf格式 及 6行代碼實現批量將word轉換爲pdf格式--python實用小技能get起來

將word轉換爲pdf格式

安裝pywin32

代碼(Anaconda終端下)python

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple --trusted-host pypi.tuna.tsinghua.edu.cn pywin32

運行結果
在這裏插入圖片描述
ui

上代碼

在pycharm中輸入代碼spa

from win32com.client import Dispatch, constants, gencache

# 放入要轉換的word格式路徑
docx_path = 'G:\示例.docx'

# 放入要導出的pdf格式的路徑,而且命名
pdf_path = 'G:\示例.pdf'

# 轉換
gencache.EnsureModule('{00020905-0000-0000-C000-000000000046}', 0, 8, 4)

wd = Dispatch('Word.Application')

doc = wd.Documents.Open(docx_path, ReadOnly=1)

doc.ExportAsFixedFormat(pdf_path, constants.wdExportFormatPDF, Item=constants.wdExportDocumentWithMarkup,
                        CreateBookmarks=constants.wdExportCreateHeadingBookmarks)

wd.Quit(constants.wdDoNotSaveChanges)

運行結果

docx文件
在這裏插入圖片描述
pdf文件
在這裏插入圖片描述
我的感受pdf版本的真的看起來好舒服呀



3d

批量實現word轉pdf

安裝docx2pdf

代碼(Anaconda終端下)code

pip install --user -i https://pypi.tuna.tsinghua.edu.cn/simple/ docx2pdf

如圖所示
在這裏插入圖片描述
orm

上代碼

from docx2pdf import convert
import os

#''放入你想要轉換的文件路徑
director = r'G:\eg'
FileList = map(lambda x: director + '\\' + x, os.listdir('G:\eg'))
for file in FileList:
    convert(file, f"{file.split('.')[0]}.pdf")

運行結果

在這裏插入圖片描述
這就是所有內容啦,但願能夠幫到你熬!在這裏插入圖片描述
blog

相關文章
相關標籤/搜索