借助pandoc将Word文档转换成网页

51cto博客地址
csdn博客地址

1、简介

​ 想要将一些已经存在的word知识文档转换成html格式,Pandoc完全可以实现此功能。

主页:https://www.pandoc.org/
示例:https://www.pandoc.org/demos.html

下载:https://github.com/jgm/pandoc/releases/

​ https://github.com/jgm/pandoc/releases/download/2.13/pandoc-2.13-windows-x86_64.msi

注:Pandoc可以实现各种各样的格式转换,详细可看此图

2、转换仅文本的word文件为html

​ 现有一个word文档“cisco.docx”,将要将它转换成html格式。

注:pandoc 不支持 doc格式,如果是 doc格式需要用 word将文件另存为 docx后再进行转换。

​ 安装好pandoc后,打开命令提示符:

D:\tt>pandoc cisco.docx -o cisco.html

image-20210421172704062

​ 命令执行完毕后会在当前目录下生成一个“cisco.html”的网页文件,我们这里是“ D:\tt ”目录。

​ 可以使用”"c:\Program Files\Internet Explorer\iexplore.exe" d:\tt\cisco.html“调用IE打开此网页文件。

image-20210421172542102

3、 转换带图片的word文件为html

​ 现有一个word文档“mail.docx”,里面有大量的图片,需要将它转换成html格式。

20210421173323.png

​ 首先使用pandoc命令进行转换:

D:\tt>pandoc Mail.docx -o mail.html

image-20210421173546283

​ 同样使用”"c:\Program Files\Internet Explorer\iexplore.exe" d:\tt\mail.html“调用IE打开此网页文件。

image-20210421174346647

​ 发现 “ mail.html ”文件中的图片都不能正常显示,查看网页的源代码,图片引用的相对路径“ media ”目录下的图片,但是我们其实是没有“ media ”目录的。

image-20210421175142956

​ 尝试将文件作为压缩文件打开(使用7-zip或者winrar都可以),发现在“ .\word ”下有个“ media ”目录,且里面有很多图片文件。

image-20210421175414437

image-20210421175621411

image-20210421175713720

将“ media ”目录整个目录解压和“ mail.html ”同级,再次调用ie打开“ mail.html ”文件查看,发现图片显示正常。

D:\tt>tree /F
文件夹 PATH 列表
卷序列号为 BA17-9E87
D:.
│  Mail.docx
│  mail.html
│
└─media
        image1.png
        image10.png
        image11.png
        image12.png
        image13.png
        image14.png
        image15.png
        image16.png
        image17.png
        image18.png
        image19.png
        image2.png
        ……
D:\tt>

image-20210421180450313

<完>

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
可以使用 Apache POI 库实现将数学公式换到 Word 文档中。具体步骤如下: 1. 首先,需要在 pom.xml 文件中添加 Apache POI 的依赖: ``` <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency> ``` 2. 创建一个 Word 文档,并添加一个段落: ``` XWPFDocument document = new XWPFDocument(); XWPFParagraph paragraph = document.createParagraph(); ``` 3. 创建一个数学公式对象,并设置公式内容: ``` XWPFMath math = paragraph.createRun().getCTR().addNewOMath(); CTOMath ctomath = CTOMath.Factory.parse("数学公式内容"); math.setOMath(ctomath); ``` 其中,"数学公式内容" 是你要插入的数学公式的内容,可以使用 LaTeX 语法编写。 4. 将 Word 文档保存到本地: ``` FileOutputStream out = new FileOutputStream("文件路径"); document.write(out); out.close(); ``` 完整代码示例: ``` import java.io.FileOutputStream; import org.apache.poi.xwpf.usermodel.*; import org.openxmlformats.schemas.officeDocument.x2006.math.CTOMath; public class MathFormulaToWord { public static void main(String[] args) throws Exception { XWPFDocument document = new XWPFDocument(); XWPFParagraph paragraph = document.createParagraph(); XWPFMath math = paragraph.createRun().getCTR().addNewOMath(); CTOMath ctomath = CTOMath.Factory.parse("数学公式内容"); math.setOMath(ctomath); FileOutputStream out = new FileOutputStream("文件路径"); document.write(out); out.close(); } } ``` 注意:在将 LaTeX 语法换为 MathML 语法时,需要借助一些工具,例如 MathJax、LaTeX2MathML 等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

堂堂糖2021

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值