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赋予动态交互。三者在网页开发中各司其职,共同构建丰富多彩的网站。

React通过JSX与HTML结合,提升用户体验。1)JSX嵌入HTML,使开发更直观。2)虚拟DOM机制优化性能,减少DOM操作。3)组件化管理UI,提高可维护性。4)状态管理和事件处理增强交互性。

MySQL 有免费的社区版和收费的企业版。社区版可免费使用和修改,但支持有限,适合稳定性要求不高、技术能力强的应用。企业版提供全面商业支持,适合需要稳定可靠、高性能数据库且愿意为支持买单的应用。选择版本时考虑的因素包括应用关键性、预算和技术技能。没有完美的选项,只有最合适的方案,需根据具体情况谨慎选择。

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性能优化的基石。以下是一些核心原则:选择合适的数据类型选择最小的、符合需求的数据类型,既能节省存储空间,又能提升数据处理速度

htmlisaspecifictypefodyfocusedonstructuringwebcontent,而“代码” badlyLyCludEslanguagesLikeLikejavascriptandPytyPythonForFunctionality.1)htmldefineswebpagertuctureduseTags.2)“代码”代码“ code” code code code codeSpassSesseseseseseseseAwiderRangeLangeLangeforLageforLogageforLogicIctInterract
