用户登录
用户注册

分享至

pythonword转html

  • 作者: LandRover3
  • 来源: 51数据库
  • 2020-04-21

一、怎么用python将word转成html

#coding=utf-8

#文件名:

#BatchConverWords2Html.py

#说明:

#批量将一个文件夹下的所有.doc/.docx文件转为.html文件,需要安装对应的win32模块

#调用方式:进入源程序目录,命令:python BatchConverWords2Html.py RootDir

from win32com import client as wc

import os

word = wc.Dispatch('Word.Application')

def wordsToHtml(dir):

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 "generate html:" + htmlFullName

doc.SaveAs(htmlFullName, 10)

doc.Close()

word.Quit()

print ""

print "Finished!"

if __name__ == '__main__':

import sys

if len(sys.argv) != 2:

print "Usage: python funcName.py rootdir"

sys.exit(100)

wordsToHtml(sys.argv[1])运行结果就是在rootdir目录下的所有word文档转为简洁版的html网页文件,生成的文件存在原word同目录下,生成 xxx.files 文件夹。

二、怎么用python 将以下形式的文档转化成 html格式

没有,需要自己解析字符,比如

# coding: utf-8

text = '''今天,晴,多云,23℃/31℃

明天,多云,中雨,25℃/31℃

后天,中雨,小雨,25℃/30℃

周一,小雨,多云,26℃/32℃

周二,多云,多云,27℃/34℃

周三,多云,多云,28℃/36℃

周四,多云,多云,28℃/36℃'''

tables = [[],[],[],[]]

for line in text.splitlines():

words = line.split(',')

for i in range(4):

tables[i].append(words[i])

print '<table>'

for tr in tables:

print '<tr>\n<td>'+'</td>\n<td>'.join(tr)+'</td>\n</tr>'

print '</table>;'截图:

三、python输出word内容

程序导出word文档的方法将web/html内容导出为world文档,再java中有很多解决方案,比如使用Jacob、Apache POI、Java2Word、iText等各种方式,以及使用freemarker这样的模板引擎这样的方式。

php中也有一些相应的方法,但在python中将web/html内容生成world文档的方法是很少的。其中最不好解决的就是如何将使用js代码异步获取填充的数据,图片导出到word文档中。

1. unoconv功能:1.支持将本地html文档转换为docx格式的文档,所以需要先将网页中的html文件保存到本地,再调用unoconv进行转换。转换效果也不错,使用方法非常简单。

\# 安装sudo apt-get install unoconv\# 使用unoconv -f pdf *.odtunoconv -f doc *.odtunoconv -f html *.odt缺点:1.只能对静态html进行转换,对于页面中有使用ajax异步获取数据的地方也不能转换(主要是要保证从web页面保存下来的html文件中有数据)。2.只能对html进行转换,如果页面中有使用echarts,highcharts等js代码生成的图片,是无法将这些图片转换到word文档中;3.生成的word文档内容格式不容易控制。

2. python-docx功能:1.python-docx是一个可以读写word文档的python库。使用方法:1.获取网页中的数据,使用python手动排版添加到word文档中。

from docx import Documentfrom docx.shared import Inchesdocument = Document()document.add_heading('Document Title', 0)p = document.add_paragraph('A plain paragraph having some ')p.add_run('bold').bold = Truep.add_run(' and some ')p.add_run('italic.').italic = Truedocument.add_heading('Heading, level 1', level=1)document.add_paragraph('Intense quote', style='IntenseQuote')document.add_paragraph('first item in unordered list', style='ListBullet')document.add_paragraph('first item in ordered list', style='ListNumber')document.add_picture('monty-truth.png', width=Inches(1.25))table = document.add_table(rows=1, cols=3)hdr_cells = table.rows[0].cellshdr_cells[0].text = 'Qty'hdr_cells[1].text = 'Id'hdr_cells[2].text = 'Desc'for item in recordset:row_cells = table.add_row().cellsrow_cells[0].text = str(item.qty)row_cells[1].text = str(item.id)row_cells[2].text = item.descdocument.add_page_break()document.save('demo.docx')from docx import Documentfrom docx.shared import Inchesdocument = Document()for row in range(9):t = document.add_table(rows=1,cols=1,style = 'Table Grid')t.autofit = False #很重要!w = float(row) / 2.0t.columns[0].width = Inches(w)document.save('table-step.docx')缺点:1.功能非常弱。有很多限制比如不支持模板等,只能生成简单格式的word文档。

程序导出PDF文档方法1.pdfkit功能:1.wkhtmltopdf主要用于HTML生成PDF。2.pdfkit是基于wkhtmltopdf的python封装,支持URL,本地文件,文本内容到PDF的转换,其最终还是调用wkhtmltopdf命令。

是目前接触到的python生成pdf效果较好的。优点:1.wkhtmltopdf:利用webkit内核将HTML转为PDFwebkit是一个高效、开源的浏览器内核,包括Chrome和Safari在内的浏览器都使用了这个内核。

Chrome打印当前网页的功能,其中有一个选项就是直接“保存为 PDF”。2.wkhtmltopdf使用webkit内核的PDF渲染引擎来将HTML页面转换为PDF。

高保真,转换质量很好,且使用非常简单。使用方法:\# 安装pip install pdfkit\# 使用import pdfkitpdfkit.from_url('', 'out.pdf')pdfkit.from_file('test.html', 'out.pdf')pdfkit.from_string('Hello!', 'out.pdf')缺点:1.对使用echarts,highcharts这样的js代码生成的图标无法转换为pdf(因为它的功能主要是将html转换为pdf,而不是将js转换为pdf)。

对于纯静态页面的转换效果还是不错的。2.其他其他生成pdf的插件还有:weasyprint,reportlab,PyPDF2等,经简单试验都不如pdfkit效果好,且有些用法复杂。

四、python输出word内容

程序导出word文档的方法将web/html内容导出为world文档,再java中有很多解决方案,比如使用Jacob、Apache POI、Java2Word、iText等各种方式,以及使用freemarker这样的模板引擎这样的方式。

php中也有一些相应的方法,但在python中将web/html内容生成world文档的方法是很少的。其中最不好解决的就是如何将使用js代码异步获取填充的数据,图片导出到word文档中。

1. unoconv功能:1.支持将本地html文档转换为docx格式的文档,所以需要先将网页中的html文件保存到本地,再调用unoconv进行转换。转换效果也不错,使用方法非常简单。

\# 安装sudo apt-get install unoconv\# 使用unoconv -f pdf *.odtunoconv -f doc *.odtunoconv -f html *.odt缺点:1.只能对静态html进行转换,对于页面中有使用ajax异步获取数据的地方也不能转换(主要是要保证从web页面保存下来的html文件中有数据)。2.只能对html进行转换,如果页面中有使用echarts,highcharts等js代码生成的图片,是无法将这些图片转换到word文档中;3.生成的word文档内容格式不容易控制。

2. python-docx功能:1.python-docx是一个可以读写word文档的python库。使用方法:1.获取网页中的数据,使用python手动排版添加到word文档中。

from docx import Documentfrom docx.shared import Inchesdocument = Document()document.add_heading('Document Title', 0)p = document.add_paragraph('A plain paragraph having some ')p.add_run('bold').bold = Truep.add_run(' and some ')p.add_run('italic.').italic = Truedocument.add_heading('Heading, level 1', level=1)document.add_paragraph('Intense quote', style='IntenseQuote')document.add_paragraph('first item in unordered list', style='ListBullet')document.add_paragraph('first item in ordered list', style='ListNumber')document.add_picture('monty-truth.png', width=Inches(1.25))table = document.add_table(rows=1, cols=3)hdr_cells = table.rows[0].cellshdr_cells[0].text = 'Qty'hdr_cells[1].text = 'Id'hdr_cells[2].text = 'Desc'for item in recordset:row_cells = table.add_row().cellsrow_cells[0].text = str(item.qty)row_cells[1].text = str(item.id)row_cells[2].text = item.descdocument.add_page_break()document.save('demo.docx')from docx import Documentfrom docx.shared import Inchesdocument = Document()for row in range(9):t = document.add_table(rows=1,cols=1,style = 'Table Grid')t.autofit = False #很重要!w = float(row) / 2.0t.columns[0].width = Inches(w)document.save('table-step.docx')缺点:1.功能非常弱。有很多限制比如不支持模板等,只能生成简单格式的word文档。

程序导出PDF文档方法1.pdfkit功能:1.wkhtmltopdf主要用于HTML生成PDF。2.pdfkit是基于wkhtmltopdf的python封装,支持URL,本地文件,文本内容到PDF的转换,其最终还是调用wkhtmltopdf命令。

是目前接触到的python生成pdf效果较好的。优点:1.wkhtmltopdf:利用webkit内核将HTML转为PDFwebkit是一个高效、开源的浏览器内核,包括Chrome和Safari在内的浏览器都使用了这个内核。

Chrome打印当前网页的功能,其中有一个选项就是直接“保存为 PDF”。2.wkhtmltopdf使用webkit内核的PDF渲染引擎来将HTML页面转换为PDF。

高保真,转换质量很好,且使用非常简单。使用方法:\# 安装pip install pdfkit\# 使用import pdfkitpdfkit.from_url('', 'out.pdf')pdfkit.from_file('test.html', 'out.pdf')pdfkit.from_string('Hello!', 'out.pdf')缺点:1.对使用echarts,highcharts这样的js代码生成的图标无法转换为pdf(因为它的功能主要是将html转换为pdf,而不是将js转换为pdf)。

对于纯静态页面的转换效果还是不错的。2.其他其他生成pdf的插件还有:weasyprint,reportlab,PyPDF2等,经简单试验都不如pdfkit效果好,且有些用法复杂。

转载请注明出处51数据库 » pythonword转html

软件
前端设计
程序设计
Java相关