从 HTML
到 Microsoft Word

2023-11-25

我有一个 HTMLfigure, img and figcaption标签,我想将它们转换为 Microsoft Word 文档。

所引用的图像img应该插入到Word文档中,并且figcaption应转换为其标题(也保留图号)。

我尝试用 Word 2013 打开 html,但是figcaption未转换为图形标题,而只是图像下方的简单文本。

是否有任何最低工作样本来完成它?我看了一下https://en.wikipedia.org/wiki/Microsoft_Office_XML_formats#Word_XML_Format_example但它太冗长了,无法仅获取一个你好世界 sample.

figure .image {
    width: 100%;
}

figure {
    text-align: center;
    display: table;
    max-width: 30%; /* demo; set some amount (px or %) if you can */
    margin: 10px auto; /* not needed unless you want centered */
}
article {
  counter-reset: figures;
}

figure {
  counter-increment: figures;
}

figcaption:before {
  content: "Fig. " counter(figures) " - "; /* For I18n support; use data-counter-string. */
}
<figure>
<p><img class="image" src="https://upload.wikimedia.org/wikipedia/commons/c/ca/Matterhorn002.jpg"></p>
<figcaption>Il monte Cervino.</figcaption>
</figure>

<figure>
<p><img class="image" src="https://upload.wikimedia.org/wikipedia/commons/2/26/Banner_clouds.jpg"></p>
<figcaption>La nuvola che spesso è vicino alla vetta.</figcaption>
</figure>

我尝试在 Windows 上使用 pandoc

pandoc -f html -t docx -o hello.docx hello.html

但不幸的是,正如您所看到的,“图 1”和“图 2”丢失了:

enter image description here

我的潘多克是:

c:\temp>.\pandoc.exe -v
pandoc.exe 1.19.2.1
Compiled with pandoc-types 1.17.0.4, texmath 0.9, skylighting 0.1.1.4
Default user data directory: C:\Users\ale\AppData\Roaming\pandoc
Copyright (C) 2006-2016 John MacFarlane
Web:  http://pandoc.org
This is free software; see the source for copying conditions.
There is no warranty, not even for merchantability or fitness
for a particular purpose.

Edit 1

使用一些 C# 来完成它也很好。也许我可以通过 C# 程序将 HTML 转换为某种 XML Word 格式。


这可能比你想要的更迂回,但如果你将文件保存为pdf(我进入adobe并从包含figure/figcaption的html文件创建了一个pdf,但你显然可以通过编程方式做到这一点),然后导出pdf文件转word,然后就可以创建word文档了。也许中间步骤太多了,但它确实有效!

希望这对您有所帮助(也许 pdf 就可以了??)

pdf (zoomed to page level

EDIT 1:我刚刚找到一个jquery插件作者:Mark Windsoll,它将 HTML 转换为 Word。我制造了一个codepen 包含图 /figcaption这里。当您按下按钮时,它会打印为 Word。 (我想你也可以保存它,但他原来的代码笔实际上在单击“导出到文档”的链接时没有执行任何操作..叹息..)

 jQuery(document).ready(function print($)  {   
$(".word-export").click(function(event) {
         $("#page-content").wordExport();
     });
 });
img{width:300px;
height:auto;}
figcaption{width:350px;text-align:center;}
h1{margin-top:10px;}
h1, h2{margin-left:35px;}
p{width:95%;
  padding-top:20px;
  margin:0px auto;}
button{margin: 15px 30px; 
padding:5px;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://www.jqueryscript.net/demo/Export-Html-To-Word-Document-With-Images-Using-jQuery-Word-Export-Plugin/FileSaver.js"></script>
<script src="https://www.jqueryscript.net/demo/Export-Html-To-Word-Document-With-Images-Using-jQuery-Word-Export-Plugin/jquery.wordexport.js"></script>

<link href="https://www.jqueryscript.net/css/jquerysctipttop.css" rel="stylesheet"/>

<h1>jQuery Word Export Plugin Demo</h1>
<div id="page-content">
<h2>Lovely Trees</h2>
<figure>
  <img src="http://www.rachelgallen.com/images/autumntrees.jpg"></figure>
  <figcaption>Autumn Trees</figcaption>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec vehicula bibendum lacinia. Pellentesque placerat interdum nisl non semper. Integer ornare, nunc non varius mattis, nulla neque venenatis nibh, vitae cursus risus quam ut nulla. Aliquam erat volutpat. Aliquam erat volutpat. </p>
  <p>And some more text here, but that's quite enough lorem ipsum rubbish!</p>
</div>
<button class="word-export" onclick="print();"> Export as .doc </button>

EDIT 2:将 HTML 转换为 Wordusing C#您可以使用Gembox,除非您购买专业版,否则它是免费的(您可以免费使用一段时间来评估它)。

C# 代码是

// Convert HTML to Word (DOCX) document.
DocumentModel.Load("Document.html").Save("Document.docx");

Rachel

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从 HTML
到 Microsoft Word 的相关文章