国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python實現批量將word轉html并將html內容發布至網站的方法

2020-01-04 18:04:47
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了Python實現批量將word轉html并將html內容發布至網站的方法,涉及Python調用第三方接口進行文件轉換及操作數據庫等相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了Python實現批量將word轉html并將html內容發布至網站的方法。分享給大家供大家參考。具體實現方法如下:

 

 
  1. #coding=utf-8 
  2. __author__ = 'zhm' 
  3. from win32com import client as wc 
  4. import os 
  5. import time 
  6. import random 
  7. import MySQLdb 
  8. import re 
  9. def wordsToHtml(dir): 
  10. #批量把文件夾的word文檔轉換成html文件 
  11. #金山WPS調用,搶先版的用KWPS,正式版WPS 
  12. word = wc.Dispatch('KWPS.Application'
  13. for path, subdirs, files in os.walk(dir): 
  14. for wordFile in files: 
  15. wordFullName = os.path.join(path, wordFile) 
  16. #print "word:" + wordFullName 
  17. doc = word.Documents.Open(wordFullName) 
  18. wordFile2 = unicode(wordFile, "gbk"
  19. dotIndex = wordFile2.rfind("."
  20. if(dotIndex == -1): 
  21. print '********************ERROR: 未取得后綴名!' 
  22. fileSuffix = wordFile2[(dotIndex + 1) : ] 
  23. if(fileSuffix == "doc" or fileSuffix == "docx"): 
  24. fileName = wordFile2[ : dotIndex] 
  25. htmlName = fileName + ".html" 
  26. htmlFullName = os.path.join(unicode(path, "gbk"), htmlName) 
  27. # htmlFullName = unicode(path, "gbk") + "//" + htmlName 
  28. print u'生成了html文件:' + htmlFullName 
  29. doc.SaveAs(htmlFullName, 8) 
  30. doc.Close() 
  31. word.Quit() 
  32. print "" 
  33. print "Finished!" 
  34. def html_add_to_db(dir): 
  35. #將轉換成功的html文件批量插入數據庫中。 
  36. conn = MySQLdb.connect( 
  37. host='localhost'
  38. port=3306, 
  39. user='root'
  40. passwd='root'
  41. db='test'
  42. charset='utf8' 
  43. cur = conn.cursor() 
  44. for path, subdirs, files in os.walk(dir): 
  45. for htmlFile in files: 
  46. htmlFullName = os.path.join(path, htmlFile) 
  47. title = os.path.splitext(htmlFile)[0] 
  48. targetDir = 'D:/files/htmls/' 
  49. #D:/files為web服務器配置的靜態目錄 
  50. sconds = time.time() 
  51. msconds = sconds * 1000 
  52. targetFile = os.path.join(targetDir, str(int(msconds))+str(random.randint(100, 10000)) +'.html'
  53. htmlFile2 = unicode(htmlFile, "gbk"
  54. dotIndex = htmlFile2.rfind("."
  55. if(dotIndex == -1): 
  56. print '********************ERROR: 未取得后綴名!' 
  57. fileSuffix = htmlFile2[(dotIndex + 1) : ] 
  58. if(fileSuffix == "htm" or fileSuffix == "html"): 
  59. if not os.path.exists(targetDir): 
  60. os.makedirs(targetDir) 
  61. htmlFullName = os.path.join(unicode(path, "gbk"), htmlFullName) 
  62. htFile = open(htmlFullName,'rb'
  63. #獲取網頁內容 
  64. htmStrCotent = htFile.read() 
  65. #找出里面的圖片 
  66. img=re.compile(r"""<img/s.*?/s?src/s*=/s*['|"]?([^/s'"]+).*?>""",re.I) 
  67. m = img.findall(htmStrCotent) 
  68. for tagContent in m: 
  69. imgSrc = unicode(tagContent, "gbk"
  70. imgSrcFullName = os.path.join(path, imgSrc) 
  71. #上傳圖片 
  72. imgTarget = 'D:/files/images/whzx/' 
  73. img_sconds = time.time() 
  74. img_msconds = sconds * 1000 
  75. targetImgFile = os.path.join(imgTarget, str(int(img_msconds))+str(random.randint(100, 10000)) +'.png'
  76. if not os.path.exists(imgTarget): 
  77. os.makedirs(imgTarget) 
  78. if not os.path.exists(targetImgFile) or(os.path.exists(targetImgFile) and (os.path.getsize(targetImgFile) != os.path.getsize(imgSrcFullName))): 
  79. tmpImgFile = open(imgSrcFullName,'rb'
  80. tmpWriteImgFile = open(targetImgFile, "wb"
  81. tmpWriteImgFile.write(tmpImgFile.read()) 
  82. tmpImgFile.close() 
  83. tmpWriteImgFile.close() 
  84. htmStrCotent=htmStrCotent.replace(tagContent,targetImgFile.split(":")[1]) 
  85. if not os.path.exists(targetFile) or(os.path.exists(targetFile) and (os.path.getsize(targetFile) != os.path.getsize(htmlFullName))): 
  86. #用iframe包裝轉換好的html文件。 
  87. iframeHtml=''
  88. <script type="text/javascript" language="javascript"
  89. function iFrameHeight() { 
  90. var ifm= document.getElementById("iframepage"); 
  91. var subWeb = document.frames ? document.frames["iframepage"].document:ifm.contentDocument; 
  92. if(ifm != null && subWeb != null) { 
  93. ifm.height = subWeb.body.scrollHeight; 
  94. </script> 
  95. <iframe src='''+targetFile.split(':')[1]+''' 
  96. marginheight="0" marginwidth="0" frameborder="0" scrolling="no" width="765" height=100% id="iframepage" name="iframepage" onLoad="iFrameHeight()" ></iframe> 
  97. ''
  98. tmpTargetFile = open(targetFile, "wb"
  99. tmpTargetFile.write(htmStrCotent) 
  100. tmpTargetFile.close() 
  101. htFile.close() 
  102. try
  103. # 執行 
  104. sql = "insert into common_article(title,content) values(%s,%s)" 
  105. param = (unicode(title, "gbk"),iframeHtml) 
  106. cur.execute(sql,param) 
  107. except: 
  108. print "Error: unable to insert data" 
  109. cur.close() 
  110. conn.commit() 
  111. # 關閉數據庫連接 
  112. conn.close() 
  113. if __name__ == '__main__'
  114. wordsToHtml('d:/word'
  115. html_add_to_db('d:/word'

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 永登县| 论坛| 宣化县| 武强县| 平安县| 上饶县| 固阳县| 平果县| 台北县| 通州市| 嘉义县| 阳城县| 历史| 浮山县| 遂溪县| 饶河县| 论坛| 湘乡市| 宁城县| 孟津县| 天镇县| 麻城市| 禹州市| 长宁区| 城市| 综艺| 沾益县| 塔城市| 娱乐| 南丹县| 武隆县| 云林县| 得荣县| 淮安市| 芦溪县| SHOW| 贵德县| 忻州市| 清远市| 礼泉县| 呼和浩特市|