欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python實(shí)現(xiàn)HTML轉(zhuǎn)Word的示例代碼

 更新時(shí)間:2023年12月30日 08:14:02   作者:E-iceblue  
這篇文章主要為大家詳細(xì)介紹了使用Python實(shí)現(xiàn)HTML轉(zhuǎn)Word的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

之前文章分享過(guò)如何使用Spire.Doc for Python庫(kù)將Word文檔轉(zhuǎn)為HTML格式,反過(guò)來(lái),該庫(kù)也能實(shí)現(xiàn)HTML到Word文檔的轉(zhuǎn)換。通過(guò)代碼進(jìn)行轉(zhuǎn)換,避免了手動(dòng)復(fù)制粘貼費(fèi)時(shí)間,并且可能會(huì)出現(xiàn)錯(cuò)誤或格式混亂等問(wèn)題。

Spire.Doc for Python庫(kù)能轉(zhuǎn)換一個(gè)HTML文件為 Word Docx 格式,也能直接將HTML字符串轉(zhuǎn)為Word文檔。具體實(shí)現(xiàn)方法查看下文。

首先通過(guò)以下pip命令安裝該P(yáng)ython庫(kù):

pip install Spire.Doc

Python 將HTML文件轉(zhuǎn)為Word

from spire.doc import *
from spire.doc.common import *
 
# 創(chuàng)建Document類的對(duì)象
document = Document()
 
# 加載一個(gè)HTML文件
document.LoadFromFile("input.html", FileFormat.Html, XHTMLValidationType.none)
 
# 將HTML文件保存為.docx格式
document.SaveToFile("Html文件轉(zhuǎn)為Word.docx", FileFormat.Docx2016)
document.Close()

上述代碼先加載了一個(gè).html文件,然后通過(guò)調(diào)用 Document.SaveToFile() 方法就將該文件轉(zhuǎn)換成了.docx 格式。三行Python代碼輕松搞定HTML文件轉(zhuǎn)Word。

效果圖:

Python 將HTML字符串轉(zhuǎn)為Word 

from spire.doc import *
from spire.doc.common import *
 
# 創(chuàng)建Document類的對(duì)象
document = Document()
 
# 在文檔中添加一節(jié)
sec = document.AddSection()
 
# 在該節(jié)中添加一個(gè)段落
paragraph = sec.AddParagraph()
 
# 指定HTML字符串
htmlString = """
<html>
<head>
    <title>HTML轉(zhuǎn)Word示例</title>
    <style>
        body {
            font-family: 微軟雅黑, sans-serif;
        }
        h1 {
            color: #CC3333;
            font-size: 24px;
            margin-bottom: 20px;
        }
        p {
            color: #333333;
            font-size: 16px;
            margin-bottom: 10px;
        }
        ul {
            list-style-type: disc;
            margin-left: 20px;
            margin-bottom: 15px;
        }
        li {
            font-size: 14px;
            margin-bottom: 5px;
        }
        table {
            border-collapse: collapse;
            width: 100%;
            margin-bottom: 20px;
        }
        th, td {
            border: 1px solid #CCCCCC;
            padding: 8px;
            text-align: left;
        }
        th {
            background-color: #F2F2F2;
            font-weight: bold;
        }
        td {
            color: #0000FF;
        }
    </style>
</head>
<body>
    <h1>標(biāo)題示例</h1>
    <p>這是一個(gè)簡(jiǎn)單段落展示。</p>
    <p>無(wú)序列表:</p>
    <ul>
        <li>數(shù)學(xué)</li>
        <li>語(yǔ)文</li>
        <li>英語(yǔ)</li>
    </ul>
    <p>表格:</p>
    <table>
        <tr>
            <th>產(chǎn)品</th>
            <th>數(shù)量</th>
            <th>價(jià)格</th>
        </tr>
        <tr>
            <td>長(zhǎng)褲</td>
            <td>30</td>
            <td>¥150</td>
        </tr>
        <tr>
            <td>毛衣</td>
            <td>2</td>
            <td>¥99</td>
        </tr>
    </table>
</body>
</html>
"""
 
# 將 HTML 字符串添加到段落中
paragraph.AppendHTML(htmlString)
 
# 保存結(jié)果文件
document.SaveToFile("Html字符串轉(zhuǎn)Word.docx", FileFormat.Docx2016)
document.Close()

上述代碼中,首先新建了一個(gè)Word文檔并添加段落,然后通過(guò) Paragraph.AppendHTML() 方法將HTML字符串插入到了Word文檔的段落中,最后再保存文檔即可將實(shí)現(xiàn)轉(zhuǎn)換。生成文件如下圖:

到此這篇關(guān)于Python實(shí)現(xiàn)HTML轉(zhuǎn)Word的示例代碼的文章就介紹到這了,更多相關(guān)Python HTML轉(zhuǎn)Word內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論