日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

编程doc转html,Python批量将word转html,并将html内容发布至网站。

發布時間:2024/9/27 python 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 编程doc转html,Python批量将word转html,并将html内容发布至网站。 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

搜索熱詞

下面是編程之家 jb51.cc 通過網絡收集整理的代碼片段。

編程之家小編現在分享給大家,也給大家做個參考。

#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,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,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,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')

以上是編程之家(jb51.cc)為你收集整理的全部代碼內容,希望文章能夠幫你解決所遇到的程序開發問題。

如果覺得編程之家網站內容還不錯,歡迎將編程之家網站推薦給程序員好友。

總結

如果覺得編程之家網站內容還不錯,歡迎將編程之家網站推薦給程序員好友。

本圖文內容來源于網友網絡收集整理提供,作為學習參考使用,版權屬于原作者。

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的编程doc转html,Python批量将word转html,并将html内容发布至网站。的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。