亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Python實(shí)現(xiàn)批量將word轉(zhuǎn)html并將html內(nèi)容發(fā)布至網(wǎng)站的方法

 更新時(shí)間:2015年07月14日 09:47:50   作者:愛兔一生  
這篇文章主要介紹了Python實(shí)現(xiàn)批量將word轉(zhuǎn)html并將html內(nèi)容發(fā)布至網(wǎng)站的方法,涉及Python調(diào)用第三方接口進(jìn)行文件轉(zhuǎn)換及操作數(shù)據(jù)庫(kù)等相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了Python實(shí)現(xiàn)批量將word轉(zhuǎn)html并將html內(nèi)容發(fā)布至網(wǎng)站的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:

#coding=utf-8
__author__ = 'zhm'
from win32com import client as wc
import os
import time
import random
import MySQLdb
import re
def wordsToHtml(dir):
#批量把文件夾的word文檔轉(zhuǎn)換成html文件
 #金山WPS調(diào)用,搶先版的用KWPS,正式版WPS
 word = wc.Dispatch('KWPS.Application')
 for path, subdirs, files in os.walk(dir):
  for wordFile in files:
   wordFullName = os.path.join(path, wordFile)
   #print "word:" + wordFullName
   doc = word.Documents.Open(wordFullName)
   wordFile2 = unicode(wordFile, "gbk")
   dotIndex = wordFile2.rfind(".")
   if(dotIndex == -1):
    print '********************ERROR: 未取得后綴名!'
   fileSuffix = wordFile2[(dotIndex + 1) : ]
   if(fileSuffix == "doc" or fileSuffix == "docx"):
    fileName = wordFile2[ : dotIndex]
    htmlName = fileName + ".html"
    htmlFullName = os.path.join(unicode(path, "gbk"), htmlName)
    # htmlFullName = unicode(path, "gbk") + "\\" + htmlName
    print u'生成了html文件:' + htmlFullName
    doc.SaveAs(htmlFullName, 8)
    doc.Close()
 word.Quit()
 print ""
 print "Finished!"
def html_add_to_db(dir):
#將轉(zhuǎn)換成功的html文件批量插入數(shù)據(jù)庫(kù)中。
 conn = MySQLdb.connect(
  host='localhost',
  port=3306,
  user='root',
  passwd='root',
  db='test',
  charset='utf8'
  )
 cur = conn.cursor()
 for path, subdirs, files in os.walk(dir):
  for htmlFile in files:
   htmlFullName = os.path.join(path, htmlFile)
   title = os.path.splitext(htmlFile)[0]
   targetDir = 'D:/files/htmls/'
   #D:/files為web服務(wù)器配置的靜態(tài)目錄
   sconds = time.time()
   msconds = sconds * 1000
   targetFile = os.path.join(targetDir, str(int(msconds))+str(random.randint(100, 10000)) +'.html')
   htmlFile2 = unicode(htmlFile, "gbk")
   dotIndex = htmlFile2.rfind(".")
   if(dotIndex == -1):
    print '********************ERROR: 未取得后綴名!'
   fileSuffix = htmlFile2[(dotIndex + 1) : ]
   if(fileSuffix == "htm" or fileSuffix == "html"):
    if not os.path.exists(targetDir):
     os.makedirs(targetDir)
    htmlFullName = os.path.join(unicode(path, "gbk"), htmlFullName)
    htFile = open(htmlFullName,'rb')
    #獲取網(wǎng)頁(yè)內(nèi)容
    htmStrCotent = htFile.read()
    #找出里面的圖片
    img=re.compile(r"""<img\s.*?\s?src\s*=\s*['|"]?([^\s'"]+).*?>""",re.I)
    m = img.findall(htmStrCotent)
    for tagContent in m:
     imgSrc = unicode(tagContent, "gbk")
     imgSrcFullName = os.path.join(path, imgSrc)
     #上傳圖片
     imgTarget = 'D:/files/images/whzx/'
     img_sconds = time.time()
     img_msconds = sconds * 1000
     targetImgFile = os.path.join(imgTarget, str(int(img_msconds))+str(random.randint(100, 10000)) +'.png')
     if not os.path.exists(imgTarget):
      os.makedirs(imgTarget)
     if not os.path.exists(targetImgFile) or(os.path.exists(targetImgFile) and (os.path.getsize(targetImgFile) != os.path.getsize(imgSrcFullName))):
      tmpImgFile = open(imgSrcFullName,'rb')
      tmpWriteImgFile = open(targetImgFile, "wb")
      tmpWriteImgFile.write(tmpImgFile.read())
      tmpImgFile.close()
      tmpWriteImgFile.close()
      htmStrCotent=htmStrCotent.replace(tagContent,targetImgFile.split(":")[1])
    if not os.path.exists(targetFile) or(os.path.exists(targetFile) and (os.path.getsize(targetFile) != os.path.getsize(htmlFullName))):
     #用iframe包裝轉(zhuǎn)換好的html文件。
     iframeHtml='''
     <script type="text/javascript" language="javascript">
      function iFrameHeight() {
       var ifm= document.getElementById("iframepage");
       var subWeb = document.frames ? document.frames["iframepage"].document:ifm.contentDocument;
       if(ifm != null && subWeb != null) {
        ifm.height = subWeb.body.scrollHeight;
       }
      }
     </script>
     <iframe src='''+targetFile.split(':')[1]+'''
      marginheight="0" marginwidth="0" frameborder="0" scrolling="no" width="765" height=100% id="iframepage" name="iframepage" onLoad="iFrameHeight()" ></iframe>
     '''
     tmpTargetFile = open(targetFile, "wb")
     tmpTargetFile.write(htmStrCotent)
     tmpTargetFile.close()
     htFile.close()
     try:
      # 執(zhí)行
      sql = "insert into common_article(title,content) values(%s,%s)"
      param = (unicode(title, "gbk"),iframeHtml)
      cur.execute(sql,param)
     except:
      print "Error: unable to insert data"
 cur.close()
 conn.commit()
 # 關(guān)閉數(shù)據(jù)庫(kù)連接
 conn.close()
if __name__ == '__main__':
 wordsToHtml('d:/word')
 html_add_to_db('d:/word')

希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • python MNIST手寫識(shí)別數(shù)據(jù)調(diào)用API的方法

    python MNIST手寫識(shí)別數(shù)據(jù)調(diào)用API的方法

    這篇文章主要介紹了python MNIST手寫識(shí)別數(shù)據(jù)調(diào)用API的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-08-08
  • Python使用OpenCV進(jìn)行標(biāo)定

    Python使用OpenCV進(jìn)行標(biāo)定

    這篇文章主要介紹了Python使用OpenCV進(jìn)行標(biāo)定,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-05-05
  • python3中rsa加密算法詳情

    python3中rsa加密算法詳情

    這篇文章主要介紹了python3中rsa加密算法詳情,rsa加密,是一種加密算法,目前而言,加密算法,是對(duì)數(shù)據(jù)、密碼等進(jìn)行加密,下文更多相關(guān)介紹,需要的小伙伴可以參考一下
    2022-05-05
  • Python 3 使用Pillow生成漂亮的分形樹圖片

    Python 3 使用Pillow生成漂亮的分形樹圖片

    這篇文章主要介紹了Python 3 使用Pillow生成漂亮的分形樹圖片,本文通過(guò)實(shí)例代碼介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-12-12
  • jupyter notebook使用argparse傳入list參數(shù)

    jupyter notebook使用argparse傳入list參數(shù)

    這篇文章主要介紹了jupyter notebook使用argparse傳入list參數(shù),jupyter notebook其實(shí)是可以使用 argparse來(lái)調(diào)用參數(shù)的,只要把參數(shù)轉(zhuǎn)為list即可,下面來(lái)看看具體的實(shí)現(xiàn)過(guò)程吧
    2022-01-01
  • python文件讀寫代碼實(shí)例

    python文件讀寫代碼實(shí)例

    這篇文章主要介紹了python文件讀寫代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-10-10
  • JupyterNotebook設(shè)置Python環(huán)境的方法步驟

    JupyterNotebook設(shè)置Python環(huán)境的方法步驟

    這篇文章主要介紹了JupyterNotebook設(shè)置Python環(huán)境的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • python設(shè)計(jì)模式大全

    python設(shè)計(jì)模式大全

    這篇文章主要介紹了python設(shè)計(jì)模式,通過(guò)簡(jiǎn)單的代碼實(shí)現(xiàn)了Python常見的各種設(shè)計(jì)模式,包括橋接模式、觀測(cè)者模式、適配器模式、工廠模式、單例模式等,需要的朋友可以參考下
    2016-06-06
  • python3 實(shí)現(xiàn)一行輸入,空格隔開的示例

    python3 實(shí)現(xiàn)一行輸入,空格隔開的示例

    今天小編就為大家分享一篇python3 實(shí)現(xiàn)一行輸入,空格隔開的示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-11-11
  • python數(shù)據(jù)庫(kù)編程 Mysql實(shí)現(xiàn)通訊錄

    python數(shù)據(jù)庫(kù)編程 Mysql實(shí)現(xiàn)通訊錄

    這篇文章主要為大家詳細(xì)介紹了python數(shù)據(jù)庫(kù)編程,Mysql實(shí)現(xiàn)通訊錄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-03-03

最新評(píng)論