python转换word到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"""""",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='''
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;
}
}
marginheight="0" marginwidth="0" frameborder="0" scrolling="no" width="765" height=100% id="iframepage" name="iframepage" onLoad="iFrameHeight()" >
'''
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转换word到html,Python实现批量将word转html并将html内容发布至网站的方法相关推荐
- python批量操作word文档实战_python批量实现Word文件转换为PDF文件
本文为大家分享了python批量转换Word文件为PDF文件的具体方法,供大家参考,具体内容如下 1.目的 通过万能的Python把一个目录下的所有Word文件转换为PDF文件. 2.遍历目录 作者总 ...
- java 中导出word后压缩文件_Java批量导出word压缩后的zip文件案例
一.js代码,由于参数比较大所以利用form表单使用post导出 function export_word(){ var selectedRows = $("#dg").datag ...
- Aspose-words结合Freemarker实现word邮件合并功能,批量处理word模板文件
最近的工作中有一个需求,需要处理word文档,有一些内容需要根据不同用户进行替换修改,使用的是word文档,替换后的内容还需要转换为pdf进行签章确认,并进行防篡改处理. 所以记录一下处理步骤,首先可 ...
- Excel转Word,Excel导出Word,利用Excel表批量生成Word文档,邮件合并进阶版
单击播放视频教材 利用Excel数据批量生成Word文档升级版,Excel转W 01需求概述 假设有图1所示的数据,需要批量生成WORD成绩通知单,通知单必须遵循图2的样式. (案例中的姓名.学校名称 ...
- word删除多余换行_批量删除Word中多余回车
有时候我们从网页上复制一些文章到Word中时,往往会带有好多向下箭头的符号,这就是软回车符号(Word中软回车是同时按住shift+enter得来的), 这些软回车占用了Word很多的空间,那么如何才 ...
- 编程doc转html,Python批量将word转html,并将html内容发布至网站。
搜索热词 下面是编程之家 jb51.cc 通过网络收集整理的代码片段. 编程之家小编现在分享给大家,也给大家做个参考. #coding=utf-8 __author__ = 'zhm' from wi ...
- Word 邮件合并功能(Excel表中指定列数据插入到Word模板指定位置,批量生成Word)
[邮件]选项卡[开始邮件合并]下拉按钮,选择[邮件合并分布向导] 在"邮件合并"任务窗格中的"选择文档类型"中选择"信函",单击" ...
- 不知道PDF怎么批量转Word?PDF批量转Word软件分享
自从我成为社畜以后,手头要处理的文件越来越多,很多PDF文件都需要我转换为Word文档再进行编辑.可是,我们逐个将PDF文件转为Word,实在是耗时又费力,如果能够将多份PDF文件进行批量转换,就会方 ...
- 如何批量复制word文件名到excel?
如何批量复制word文件名到excel?批量复制word文件的名称到excel表格中,这个操作最突出的重点是批量,也就是我们常说的批量处理,批量处理能帮助我们提高效率,很多人不知道如何批量复制,那么就 ...
- 如何批量处理word中的表格
如何批量处理word中的表格 如何批量处理word中的表格 1 . 批量设置word中的表格宽度 比如,我这有一个word文档有200多个表格(全部是3列的),现在要求第一列宽度3cm,第二列4cm, ...
最新文章
- 修改SQL SERVER内置存储过程
- 十一月工作小记--上线前的冲刺
- mysql中鼠标光标消失了_为什么我这里没有显示鼠标的悬停可改变页面颜色,以为什么我加载了mysql的jar文件还是不能显示报表的内容呢?...
- TensorFlow入门:Graph
- FCKeditor所有版本任意文件上传缺陷
- netstat命令查看套接字信息
- bzoj 2535: [Noi2010]Plane 航空管制2【拓扑排序+堆】
- 登录失败时记住访问的地址
- Android binder 框架和学习资料
- 非结构化WEB数据库与图书馆多媒体信息资源的组织利用
- hdu 5245Joyful 容斥
- 计算机管理中无法格式化,Win7电脑无法格式化SD卡怎么办?
- 卫星通信系统按照工作轨道分类
- 树状笔记软件for linux,WikidPad:wiki风格的开源树状笔记管理软件(新增发布内容为html)...
- js图片轮播指示器箭头淡入淡出
- 什么是架构?架构师的职责是什么?
- index.php g wap,代码阅读--wap端入口文件index.php
- leetcode系列-链表
- 【笔记】识别手写数字神经网络
- 蓝桥杯NE555模块编程