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

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

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

本文實例講述了Python實現(xiàn)批量將word轉(zhuǎn)html并將html內(nèi)容發(fā)布至網(wǎng)站的方法。分享給大家供大家參考。具體實現(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ù)庫中。
 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)頁內(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ù)庫連接
 conn.close()
if __name__ == '__main__':
 wordsToHtml('d:/word')
 html_add_to_db('d:/word')

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

相關(guān)文章

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

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

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

    python使用matplotlib畫柱狀圖、散點圖

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

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

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

    Python裝飾器原理與簡單用法實例分析

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

    Python異常處理例題整理

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

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

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

    pycharm中安裝git遇到的問題及解決

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

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

    今天小編就為大家分享一篇python控制windows剪貼板,向剪貼板中寫入圖片的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    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)換的問題

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

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

最新評論

日喀则市| 长顺县| 安溪县| 博野县| 翁牛特旗| 海城市| 大同县| 临清市| 临清市| 丹巴县| 察雅县| 三江| 重庆市| 赣榆县| 万盛区| 南阳市| 揭阳市| 石屏县| 自治县| 河南省| 安顺市| 大冶市| 承德市| 安西县| 九龙县| 尤溪县| 乐东| 平邑县| 巍山| 墨玉县| 平定县| 茶陵县| 太仆寺旗| 彭阳县| 贺州市| 三门县| 虎林市| 石林| 上虞市| 新野县| 沙河市|