Python實(shí)現(xiàn)批量將word轉(zhuǎn)html并將html內(nèi)容發(fā)布至網(wǎng)站的方法
本文實(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ì)有所幫助。
- python將html轉(zhuǎn)成PDF的實(shí)現(xiàn)代碼(包含中文)
- Python實(shí)現(xiàn)抓取HTML網(wǎng)頁(yè)并以PDF文件形式保存的方法
- Python實(shí)現(xiàn)將HTML轉(zhuǎn)成PDF的方法分析
- python 將html轉(zhuǎn)換為pdf的幾種方法
- Python3轉(zhuǎn)換html到pdf的不同解決方案
- Python實(shí)現(xiàn)html轉(zhuǎn)換為pdf報(bào)告(生成pdf報(bào)告)功能示例
- python包pdfkit(wkhtmltopdf)?將HTML轉(zhuǎn)換為PDF的操作方法
- python如何實(shí)現(xiàn)word批量轉(zhuǎn)HTML
- 如何利用Python將html轉(zhuǎn)為pdf、word文件
相關(guān)文章
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-08jupyter 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-01JupyterNotebook設(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-12python3 實(shí)現(xiàn)一行輸入,空格隔開的示例
今天小編就為大家分享一篇python3 實(shí)現(xiàn)一行輸入,空格隔開的示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-11-11python數(shù)據(jù)庫(kù)編程 Mysql實(shí)現(xiàn)通訊錄
這篇文章主要為大家詳細(xì)介紹了python數(shù)據(jù)庫(kù)編程,Mysql實(shí)現(xiàn)通訊錄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03