python批量實(shí)現(xiàn)Word文件轉(zhuǎn)換為PDF文件
本文為大家分享了python批量轉(zhuǎn)換Word文件為PDF文件的具體方法,供大家參考,具體內(nèi)容如下
1、目的
通過萬能的Python把一個(gè)目錄下的所有Word文件轉(zhuǎn)換為PDF文件。
2、遍歷目錄
作者總結(jié)了三種遍歷目錄的方法,分別如下。
2.1.調(diào)用glob
遍歷指定目錄下的所有文件和文件夾,不遞歸遍歷,需要手動(dòng)完成遞歸遍歷功能。
import glob as gb path = gb.glob('d:\\2\\*') for path in path: print path
2.2.調(diào)用os.walk
遍歷指定目錄下的所有文件和文件夾,遞歸遍歷,功能強(qiáng)大,推薦使用。
import os for dirpath, dirnames, filenames in os.walk('d:\\2\\'): for file in filenames: fullpath = os.path.join(dirpath, file) print fullpath, file
2.3.自己DIY
遍歷指定目錄下的所有文件和文件夾,遞歸遍歷,自主編寫,擴(kuò)展性強(qiáng),可以學(xué)習(xí)練手。
import os; files = list(); def DirAll(pathName): if os.path.exists(pathName): fileList = os.listdir(pathName); for f in fileList: if f=="$RECYCLE.BIN" or f=="System Volume Information": continue; f=os.path.join(pathName,f); if os.path.isdir(f): DirAll(f); else: dirName=os.path.dirname(f); baseName=os.path.basename(f); if dirName.endswith(os.sep): files.append(dirName+baseName); else: files.append(dirName+os.sep+baseName); DirAll("D:\\2\\"); for f in files: print f # print f.decode('gbk').encode('utf-8');
2.4.備注
注意,如果遍歷過程中,出現(xiàn)文件名稱或文件路徑亂碼問題,可以查看本文的參考資料來解決。
3、轉(zhuǎn)換Word文件為PDF
通過Windows Com組件(win32com),調(diào)用Word服務(wù)(Word.Application),實(shí)現(xiàn)Word到PDF文件的轉(zhuǎn)換。因此,要求該P(yáng)ython程序需要在有Word服務(wù)(可能至少要求2007版本)的Windows機(jī)器上運(yùn)行。
#coding:utf8 import os, sys reload(sys) sys.setdefaultencoding('utf8') from win32com.client import Dispatch, constants, gencache input = 'D:\\2\\test\\11.docx' output = 'D:\\2\\test\\22.pdf' print 'input file', input print 'output file', output # enable python COM support for Word 2007 # this is generated by: makepy.py -i "Microsoft Word 12.0 Object Library" gencache.EnsureModule('{00020905-0000-0000-C000-000000000046}', 0, 8, 4) # 開始轉(zhuǎn)換 w = Dispatch("Word.Application") try: doc = w.Documents.Open(input, ReadOnly=1) doc.ExportAsFixedFormat(output, constants.wdExportFormatPDF, \ Item=constants.wdExportDocumentWithMarkup, CreateBookmarks=constants.wdExportCreateHeadingBookmarks) except: print ' exception' finally: w.Quit(constants.wdDoNotSaveChanges) if os.path.isfile(output): print 'translate success' else: print 'translate fail'
4、批量轉(zhuǎn)換
要實(shí)現(xiàn)批量準(zhǔn)換,將第2步和第3步的功能組合在一起即可,直接上代碼。
# -*- coding:utf-8 -*- # doc2pdf.py: python script to convert doc to pdf with bookmarks! # Requires Office 2007 SP2 # Requires python for win32 extension import glob as gb import sys reload(sys) sys.setdefaultencoding('utf8') ''' 參考:http://blog.csdn.net/rumswell/article/details/7434302 ''' import sys, os from win32com.client import Dispatch, constants, gencache # from config import REPORT_DOC_PATH,REPORT_PDF_PATH REPORT_DOC_PATH = 'D:/2/doc/' REPORT_PDF_PATH = 'D:/2/doc/' # Word轉(zhuǎn)換為PDF def word2pdf(filename): input = filename + '.docx' output = filename + '.pdf' pdf_name = output # 判斷文件是否存在 os.chdir(REPORT_DOC_PATH) if not os.path.isfile(input): print u'%s not exist' % input return False # 文檔路徑需要為絕對(duì)路徑,因?yàn)閃ord啟動(dòng)后當(dāng)前路徑不是調(diào)用腳本時(shí)的當(dāng)前路徑。 if (not os.path.isabs(input)): # 判斷是否為絕對(duì)路徑 # os.chdir(REPORT_DOC_PATH) input = os.path.abspath(input) # 返回絕對(duì)路徑 else: print u'%s not absolute path' % input return False if (not os.path.isabs(output)): os.chdir(REPORT_PDF_PATH) output = os.path.abspath(output) else: print u'%s not absolute path' % output return False try: print input, output # enable python COM support for Word 2007 # this is generated by: makepy.py -i "Microsoft Word 12.0 Object Library" gencache.EnsureModule('{00020905-0000-0000-C000-000000000046}', 0, 8, 4) # 開始轉(zhuǎn)換 w = Dispatch("Word.Application") try: doc = w.Documents.Open(input, ReadOnly=1) doc.ExportAsFixedFormat(output, constants.wdExportFormatPDF, \ Item=constants.wdExportDocumentWithMarkup, CreateBookmarks=constants.wdExportCreateHeadingBookmarks) except: print ' exception' finally: w.Quit(constants.wdDoNotSaveChanges) if os.path.isfile(pdf_name): print 'translate success' return True else: print 'translate fail' return False except: print ' exception' return -1 if __name__ == '__main__': # img_path = gb.glob(REPORT_DOC_PATH + "*") # for path in img_path: # print path # rc = word2pdf(path) # rc = word2pdf('1') # print rc, # if rc: # sys.exit(rc) # sys.exit(0) import os for dirpath, dirnames, filenames in os.walk(REPORT_DOC_PATH): for file in filenames: fullpath = os.path.join(dirpath, file) print fullpath, file rc = word2pdf(file.rstrip('.docx'))
5、參考資料
利用Python將word 2007的文檔轉(zhuǎn)為pdf文件
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 利用python程序生成word和PDF文檔的方法
- python word轉(zhuǎn)pdf代碼實(shí)例
- 詳解用Python把PDF轉(zhuǎn)為Word方法總結(jié)
- 一文教會(huì)你用Python實(shí)現(xiàn)pdf轉(zhuǎn)word
- Python實(shí)現(xiàn)批量word文檔轉(zhuǎn)pdf并統(tǒng)計(jì)其頁碼
- Python實(shí)現(xiàn)批量將word轉(zhuǎn)換成pdf
- Python實(shí)現(xiàn)PDF轉(zhuǎn)Word的多種方式總結(jié)
- Python將Word文檔轉(zhuǎn)為PDF的兩種方法
相關(guān)文章
Python 創(chuàng)建守護(hù)進(jìn)程的示例
這篇文章主要介紹了Python 創(chuàng)建守護(hù)進(jìn)程的示例,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2020-09-09Python實(shí)現(xiàn)求解最大公約數(shù)的五種方法總結(jié)
求最大公約數(shù)是習(xí)題中比較常見的類型,本文小編將給大家提供五種比較常見的算法,都是用Python語言實(shí)現(xiàn)的,感興趣的小伙伴可以了解一下2022-07-07python語法學(xué)習(xí)print中f-string用法示例
這篇文章主要為大家介紹了python語法學(xué)習(xí)print中f-string用法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03Python-while 計(jì)算100以內(nèi)奇數(shù)和的方法
今天小編就為大家分享一篇Python-while 計(jì)算100以內(nèi)奇數(shù)和的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-06-06python+unittest+requests實(shí)現(xiàn)接口自動(dòng)化的方法
這篇文章主要介紹了python+unittest+requests實(shí)現(xiàn)接口自動(dòng)化的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-11-11PyTorch中flatten()?函數(shù)的用法實(shí)例小結(jié)
在PyTorch中,flatten函數(shù)的作用是將一個(gè)多維的張量轉(zhuǎn)換為一維的向量,它可以將任意形狀的張量轉(zhuǎn)換為一維,而不需要指定轉(zhuǎn)換后的大小,這篇文章主要介紹了PyTorch中flatten()?函數(shù)的用法,需要的朋友可以參考下2023-11-11