在日常办公或学习中,我们经常需要将网页上的内容保存为 Word 文档(.doc 或 .docx 格式),以便离线查看、编辑或打印。本文将介绍几种简单有效的方法,帮助你轻松实现网页转 Word。
这是最通用的方法,适用于所有网页:
Ctrl + P(Windows)或 Cmd + P(Mac)打开打印对话框;如果你是网站开发者,可以使用开源库如 html-docx-js 或 Mammoth.js 将 HTML 内容转换为 .docx 文件。
示例代码(使用 html-docx-js):
<script src="https://unpkg.com/html-docx-js@0.1.5/dist/html-docx.js"></script>
<script>
const html = '<h1>Hello World</h1><p>This is a test.</p>';
const docx = htmlDocx.asBlob(html);
const link = document.createElement('a');
link.href = URL.createObjectURL(docx);
link.download = 'export.docx';
link.click();
</script>
对于普通用户,可直接复制网页内容粘贴到在线 Word 编辑器,或使用专门的转换服务: