最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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模塊導(dǎo)入的幾種方法實(shí)現(xiàn)

    Python模塊導(dǎo)入的幾種方法實(shí)現(xiàn)

    本文主要介紹了Python模塊導(dǎo)入的幾種方法實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2024-12-12
  • python使用matplotlib畫柱狀圖、散點(diǎn)圖

    python使用matplotlib畫柱狀圖、散點(diǎn)圖

    這篇文章主要為大家詳細(xì)介紹了python使用matplotlib畫柱狀圖、散點(diǎn)圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-03-03
  • TensorFlow索引與切片的實(shí)現(xiàn)方法

    TensorFlow索引與切片的實(shí)現(xiàn)方法

    這篇文章主要介紹了TensorFlow索引與切片的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • Python裝飾器原理與簡(jiǎn)單用法實(shí)例分析

    Python裝飾器原理與簡(jiǎn)單用法實(shí)例分析

    這篇文章主要介紹了Python裝飾器原理與簡(jiǎn)單用法,結(jié)合實(shí)例形式分析了Python裝飾器的概念、原理、使用方法及相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2018-04-04
  • Python異常處理例題整理

    Python異常處理例題整理

    在本篇文章里
    2019-07-07
  • Python閉包函數(shù)定義與用法分析

    Python閉包函數(shù)定義與用法分析

    這篇文章主要介紹了Python閉包函數(shù)定義與用法,結(jié)合實(shí)例形式分析了Python閉包函數(shù)的功能、定義、使用方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下
    2018-07-07
  • pycharm中安裝git遇到的問(wèn)題及解決

    pycharm中安裝git遇到的問(wèn)題及解決

    在PyCharm中安裝Git時(shí)遇到問(wèn)題,按照視頻步驟操作后發(fā)現(xiàn)沒有g(shù)it選項(xiàng),重新檢查設(shè)置,發(fā)現(xiàn)git目錄配置錯(cuò)誤,重新選擇正確的目錄后,通過(guò)Test確認(rèn)無(wú)誤,在使用commit提交時(shí)遇到錯(cuò)誤,按刷新按鈕即可解決
    2024-11-11
  • python控制windows剪貼板,向剪貼板中寫入圖片的實(shí)例

    python控制windows剪貼板,向剪貼板中寫入圖片的實(shí)例

    今天小編就為大家分享一篇python控制windows剪貼板,向剪貼板中寫入圖片的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-05-05
  • Python教程pandas數(shù)據(jù)分析去重復(fù)值

    Python教程pandas數(shù)據(jù)分析去重復(fù)值

    Pandas指定行進(jìn)行去重更新值,加載數(shù)據(jù)sample抽樣函數(shù),指定需要更新的值append直接添加append函數(shù)用法,根據(jù)某一列key值進(jìn)行去重key唯一
    2021-09-09
  • 關(guān)于numpy強(qiáng)制類型轉(zhuǎn)換的問(wèn)題

    關(guān)于numpy強(qiáng)制類型轉(zhuǎn)換的問(wèn)題

    這篇文章主要介紹了關(guān)于numpy強(qiáng)制類型轉(zhuǎn)換的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-05-05

最新評(píng)論

安吉县| 蕉岭县| 电白县| 东莞市| 洪雅县| 西畴县| 信宜市| 黄浦区| 离岛区| 定州市| 赞皇县| 阿瓦提县| 苏州市| 南投县| 南昌市| 广东省| 和顺县| 青河县| 敦煌市| 廉江市| 稷山县| 南京市| 珠海市| 乐昌市| 石阡县| 汶川县| 苏尼特左旗| 开阳县| 澜沧| 新化县| 藁城市| 海安县| 永川市| 镇巴县| 天镇县| 宾阳县| 井陉县| 清水河县| 汝城县| 钦州市| 开平市|