Python实现批量将word转html并将html内容发布至网站的方法
本文实例讲述了Python实现批量将word转html并将html内容发布至网站的方法。分享给大家供大家参考。具体实现方法如下:
#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文档转换成html文件 #金山WPS调用,抢先版的用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): #将转换成功的html文件批量插入数据库中。 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服务器配置的静态目录 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') #获取网页内容 htmStrCotent = htFile.read() #找出里面的图片 img=re.compile(r"""<img \s.*?\s?src\s*=\s*['|"]?([^\s'"]+).*? alt="Python实现批量将word转html并将html内容发布至网站的方法" >""",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包装转换好的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: # 执行 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() # 关闭数据库连接 conn.close() if __name__ == '__main__': wordsToHtml('d:/word') html_add_to_db('d:/word')
希望本文所述对大家的Python程序设计有所帮助。

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

HTML定義網頁結構,CSS負責樣式和佈局,JavaScript賦予動態交互。三者在網頁開發中各司其職,共同構建豐富多彩的網站。

MySQL 有免費的社區版和收費的企業版。社區版可免費使用和修改,但支持有限,適合穩定性要求不高、技術能力強的應用。企業版提供全面商業支持,適合需要穩定可靠、高性能數據庫且願意為支持買單的應用。選擇版本時考慮的因素包括應用關鍵性、預算和技術技能。沒有完美的選項,只有最合適的方案,需根據具體情況謹慎選擇。

React通過JSX與HTML結合,提升用戶體驗。 1)JSX嵌入HTML,使開發更直觀。 2)虛擬DOM機制優化性能,減少DOM操作。 3)組件化管理UI,提高可維護性。 4)狀態管理和事件處理增強交互性。

HadiDB:輕量級、高水平可擴展的Python數據庫HadiDB(hadidb)是一個用Python編寫的輕量級數據庫,具備高度水平的可擴展性。安裝HadiDB使用pip安裝:pipinstallhadidb用戶管理創建用戶:createuser()方法創建一個新用戶。 authentication()方法驗證用戶身份。 fromhadidb.operationimportuseruser_obj=user("admin","admin")user_obj.

直接通過 Navicat 查看 MongoDB 密碼是不可能的,因為它以哈希值形式存儲。取回丟失密碼的方法:1. 重置密碼;2. 檢查配置文件(可能包含哈希值);3. 檢查代碼(可能硬編碼密碼)。

MySQL Workbench 可以連接 MariaDB,前提是配置正確。首先選擇 "MariaDB" 作為連接器類型。在連接配置中,正確設置 HOST、PORT、USER、PASSWORD 和 DATABASE。測試連接時,檢查 MariaDB 服務是否啟動,用戶名和密碼是否正確,端口號是否正確,防火牆是否允許連接,以及數據庫是否存在。高級用法中,使用連接池技術優化性能。常見錯誤包括權限不足、網絡連接問題等,調試錯誤時仔細分析錯誤信息和使用調試工具。優化網絡配置可以提升性能

MySQL數據庫性能優化指南在資源密集型應用中,MySQL數據庫扮演著至關重要的角色,負責管理海量事務。然而,隨著應用規模的擴大,數據庫性能瓶頸往往成為製約因素。本文將探討一系列行之有效的MySQL性能優化策略,確保您的應用在高負載下依然保持高效響應。我們將結合實際案例,深入講解索引、查詢優化、數據庫設計以及緩存等關鍵技術。 1.數據庫架構設計優化合理的數據庫架構是MySQL性能優化的基石。以下是一些核心原則:選擇合適的數據類型選擇最小的、符合需求的數據類型,既能節省存儲空間,又能提升數據處理速度

無法連接 MySQL 可能是由於以下原因:MySQL 服務未啟動、防火牆攔截連接、端口號錯誤、用戶名或密碼錯誤、my.cnf 中的監聽地址配置不當等。排查步驟包括:1. 檢查 MySQL 服務是否正在運行;2. 調整防火牆設置以允許 MySQL 監聽 3306 端口;3. 確認端口號與實際端口號一致;4. 檢查用戶名和密碼是否正確;5. 確保 my.cnf 中的 bind-address 設置正確。
