word - python如何在docx中插入带css样式的html代码
巴扎黑
巴扎黑 2017-04-18 10:09:06
[Python讨论组]

例如 <b>aaa</b><span style='color:red'>bbb</span>
插入word中,显示为加粗的aaa和红色的bbb
html标签上会有简单的内联css

巴扎黑
巴扎黑

全部回复(1)
巴扎黑

可以下载python的这个WordInserter。python处理word excel有很多包,可以到官网搜索一下,搜索之前考虑下是win下还是linux下用的,因为有些包因为接口问题不是跨平台的。

传送门

from wordinserter import render, parse
from comtypes.client import CreateObject

# This opens Microsoft Word and creates a new document.
word = CreateObject("Word.Application")
word.Visible = True # Don't set this to True in production!
document = word.Documents.Add()
from comtypes.gen import Word as constants

html = """
<h3>This is a title</h3>
<p><img src="http://placehold.it/150x150" alt="I go below the image as a caption"></p>
<p><i>This is <b>some</b> text</i> in a <a href="http://google.com">paragraph</a></p>
<ul>
    <li>Boo! I am a <b>list</b></li>
</ul>
"""

markdown = """
### This is a title

![I go below the image as a caption](http://placehold.it/150x150)

*This is **some** text* in a [paragraph](http://google.com)

  * Boo! I'm a **list**
"""

# Parse the HTML into a list of operations then feed them into render.
# The Markdown can be parsed by using parser="markdown"
operations = parse(html, parser="html")
render(operations, document=document, constants=constants)
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号