pythonhtml生成word_html转word

本文介绍了在Python中将HTML内容转换为Word文档的两种方法:unoconv和python-docx。unoconv适用于静态HTML转换,但无法处理异步数据和js生成的图片。而python-docx库能创建简单格式的Word文档,但功能有限,需要手动排版。通过示例代码展示了如何使用python-docx创建和操作Word文档。
部署运行你感兴趣的模型镜像

html转word

将web/html内容导出为world文档,再java中有很多解决方案,比如使用Jacob、Apache POI、Java2Word、iText等各种方式,以及使用freemarker这样的模板引擎这样的方式。php中也有一些相应的方法,但在python中将web/html内容生成world文档的方法是很少的。其中最不好解决的就是如何将使用js代码异步获取填充的数据,图片导出到word文档中。

unoconv

优缺点

功能

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

缺点

1.只能对静态html进行转换,对于页面中有使用ajax异步获取数据的地方也不能转换(主要是要保证从web页面保存下来的html文件中有数据)。

2.只能对html进行转换,如果页面中有使用echarts,highcharts等js代码生成的图片,是无法将这些图片转换到word文档中;

3.生成的word文档内容格式不容易控制。

使用

# 安装

sudo apt-get install unoconv

# 使用

unoconv -f pdf *.odt

unoconv -f doc *.odt

unoconv -f html *.odt

python-docx

优缺点

功能

1.python-docx是一个可以读写word文档的python库。

缺点

1.功能非常弱。有很多限制比如不支持模板等,只能生成简单格式的word文档。

使用方法

获取网页中的数据,使用python手动排版添加到word文档中。

from docx import Document

from docx.shared import Inches

document = Document()

document.add_heading('Document Title', 0)

p = document.add_paragraph('A plain paragraph having some ')

p.add_run('bold').bold = True

p.add_run(' and some ')

p.add_run('italic.').italic = True

document.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].cells

hdr_cells[0].text = 'Qty'

hdr_cells[1].text = 'Id'

hdr_cells[2].text = 'Desc'

for item in recordset:

row_cells = table.add_row().cells

row_cells[0].text = str(item.qty)

row_cells[1].text = str(item.id)

row_cells[2].text = item.desc

document.add_page_break()

document.save('demo.docx')

from docx import Document

from docx.shared import Inches

document = Document()

for row in range(9):

t = document.add_table(rows=1,cols=1,style = 'Table Grid')

t.autofit = False #很重要!

w = float(row) / 2.0

t.columns[0].width = Inches(w)

document.save('table-step.docx')

您可能感兴趣的与本文相关的镜像

Python3.11

Python3.11

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值