老熟女激烈的高潮_日韩一级黄色录像_亚洲1区2区3区视频_精品少妇一区二区三区在线播放_国产欧美日产久久_午夜福利精品导航凹凸

使用python怎么批量將Word文件轉(zhuǎn)換為PDF文件-創(chuàng)新互聯(lián)

這篇文章主要介紹了使用python怎么批量將Word文件轉(zhuǎn)換為PDF文件,創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計(jì)公司小編覺(jué)得不錯(cuò),現(xiàn)在分享給大家,也給大家做個(gè)參考,一起跟隨創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計(jì)公司小編來(lái)看看吧!

在慈溪等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì) 網(wǎng)站設(shè)計(jì)制作按需網(wǎng)站策劃,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),成都品牌網(wǎng)站建設(shè),網(wǎng)絡(luò)營(yíng)銷推廣,外貿(mào)網(wǎng)站建設(shè),慈溪網(wǎng)站建設(shè)費(fèi)用合理。

python可以做什么

Python是一種編程語(yǔ)言,內(nèi)置了許多有效的工具,Python幾乎無(wú)所不能,該語(yǔ)言通俗易懂、容易入門、功能強(qiáng)大,在許多領(lǐng)域中都有廣泛的應(yīng)用,例如最熱門的大數(shù)據(jù)分析,人工智能,Web開(kāi)發(fā)等。

調(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.備注

注意,如果遍歷過(guò)程中,出現(xiàn)文件名稱或文件路徑亂碼問(wèn)題,可以查看本文的參考資料來(lái)解決。

3、轉(zhuǎn)換Word文件為PDF

通過(guò)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)
# 開(kāi)始轉(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)
  # 開(kāi)始轉(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'))

以上就是創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計(jì)公司小編為大家收集整理的使用python怎么批量將Word文件轉(zhuǎn)換為PDF文件,如何覺(jué)得創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計(jì)公司網(wǎng)站的內(nèi)容還不錯(cuò),歡迎將創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計(jì)公司網(wǎng)站推薦給身邊好友。

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、網(wǎng)站設(shè)計(jì)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。


本文標(biāo)題:使用python怎么批量將Word文件轉(zhuǎn)換為PDF文件-創(chuàng)新互聯(lián)
文章源于:http://www.xueling.net.cn/article/dhdsjj.html

其他資訊

在線咨詢
服務(wù)熱線
服務(wù)熱線:028-86922220
TOP
主站蜘蛛池模板: www.黄色网| 中文字幕久久一区 | 亚洲国产国语自产精品 | 日本高清免费看 | 啪啪一区二区三区 | 国产精品97色色 | 性少妇tubevⅰdeos高清 | 黑人巨大VS苍井空 | 欧美成人激情在线 | 羞羞影院午夜男女爽爽视频免费 | 日韩一区二区三 | 一级特黄色大片 | 一区播放 | 国产精品久久久久久久久久久丝袜 | 粉色视频在线免费观看 | 欧美精品cum4k高潮 | 级毛片内射人与曾 | 亚欧视频在线观看 | 青青青在线视频观看 | 无码精品一区二区三区在线 | 国产揄拍国产精品人妻蜜 | 潘金莲性xxxxhd | 久久网一区二区 | 亚洲色精品三区二区一区 | 天天爱天天草 | 国产精品网站在线免费观看 | 一色屋精品视频在线观看免费 | 日本久久99 | 成人性免费视频 | 欧美视频亚洲色图 | 国产精品美女视频网站 | 久久香蕉成人免费大片 | 国产日产欧产精品精乱了派 | 9999国产精品欧美久久久久久 | 操操操夜夜操 | 精品久久久久人成 | 色婷婷久久一区二区三区麻豆 | 88国产精品视频一区二区三区 | 国产精品三区在线观看 | h视频在线观看网站 | 久久综合国产精品台湾中文娱乐网 |