您的位置:首页 > 资讯 > 体育新闻 > 正文

java使用pandoc将markdown转换为word文档

作者:菜叶 时间:2023-01-17 03:06

简介:大纲pandoc 下载和基本使用java 操作pandoc 批量转换为word文档使用screw生成数据库文档场景概述在项目开发时,平时喜欢用

【菜叶百科解读】

pandoc 下载和基本使用

java 操作pandoc 批量转换为word文档

使用screw生成数据库文档

场景概述

在项目开发时,平时喜欢用markdown写文档,但项目交付的时候,需要转换为更加正式的word文档进行交付. 现在考虑用pandoc将.md文件转换为word文档,文档目录结构如下:

dev-docs|__docs|____docsify |____imgs文档图片存放|____core |____other _sidebar.md md文档目录 pandoc 下载和基本使用

官网下载pandoc: https://www.pandoc.org/installing.html

Pandoc 可以用于各种文档格式之间的转换。如: Markdown、Microsoft Word、HTML、EPUB、roff man、LaTeX、PDF。

命令行使用示例: https://www.pandoc.org/demos.html

// 1 .md 文件转换为word文档pandoc test.md -o newtest.docx// 2 多个md转换为一个word文档,pandoc xx1.md xx2.md -o 合并后的.docx /**3 自定义文档格模板格式**///3.1 首先获取pandoc 自带的doc模板文档muban.docxpandoc.exe -o muban.docx --print-default-data-file=reference.docx//3.2 打开muban.docx,修改模板文件中样式(如下图,需要通过右键修改样式方式,模板才能生效)//3.3 使用模板文档,进行转换pandoc.exe xx1.md xx2.md -o 合并后的.docx --reference-doc=C:reference.docx

修改调整后的模板文档 muban.docx 「链接」

java使用pandoc将markdown转换为word文档

需要通过右键修改样式方式,模板才能生效

markdown转word样式没处理好,生成word后新建样式手动处理

java使用pandoc将markdown转换为word文档

通过名称可以控制样式排序

java使用pandoc将markdown转换为word文档

段落背景色设置

java使用pandoc将markdown转换为word文档

前后缩进控制

java 操作pandoc 批量转换为word文档

效果图预览:

java使用pandoc将markdown转换为word文档

markdown文档

java使用pandoc将markdown转换为word文档

转换后word效果图

代码如下

/** * 通过pandoc输出默认模板到custom.docx: pandoc.exe -o custom.docx --print-default-data-file=reference.docx * <p> * 使用指定的文件作为输出文件的格式参考。参考文件的内容被忽略,只使用其中的样式和文档属性(包括边界、页面尺寸、页眉页脚等) (--reference-doc=File(custom.docx)) 指定要生成的word模板 */public class Markdown2Docx_backup { //pandoc.exe 文件绝对路径 private static final String PANDOC_PATH = "C:docpandoc.exe "; //markdown文档(.md后缀格式) 所在路径 private static final String DOCS_DIR = "C:docdocsdocs"; //文档依赖图片路径 private static final String IMG_DIR = "C:docdocsdocsimgs"; //侧边栏_sidebar.md,根据侧边栏顺序转换文档 private static final String _sidebar = "C:docdocsdocs_sidebar.md"; //docx模板 使用 pandoc 进行文档转换(markdown转word) private static final String reference_docx = "C:docdocsdocsreference.docx"; public static void main(String[] args) { copyImageDir(); String mdFilePath = buildAllMdFilePath(); convertmd2Docx(mdFilePath); } /** * 解析侧边栏 _sidebar.md 生成所有markdown文件路径,如: xx1.md xx2.md * <p> * 侧边栏内容示例: -[文档说明](/job-debug.md) */ private static final String buildAllMdFilePath() { StringBuilder mds = new StringBuilder(); File sidebarFile = new File(_sidebar); if (!sidebarFile.exists()) { System.err.println("_sidebar.md 侧边栏文件不存在"); return null; } //获取文件首行作为文件名 List<String> contents = FileUtil.readTxtFile(_sidebar, "utf-8"); for (String content : contents) { //content示例必须有()将md路径包含: -[文档说明](/job-debug.md) try { if (StringUtil.isNullOrEmpty(content.trim())) { continue; } if (content.indexOf("](") < 0) { System.out.println(content + ", 不是markdown 路径不进行转换"); continue; } //解析出 /job-debug.md String mdPath = content.split("](")[1].replace(")", "").replace("/", "").trim(); if (mdPath.endsWith(".md")) { mds.append(DOCS_DIR).append("").append(mdPath).append(" "); } else { mds.append(DOCS_DIR).append("").append(mdPath).append(".md").append(" "); } } catch (Exception e) { System.err.println("从文档中解析md文件路径失败"); } } return mds.toString(); } private static void convertmd2Docx(String mdFilePath) { String docxPath = DOCS_DIR + "开发手册.docx"; Runtime rn = Runtime.getRuntime(); try { //pandoc xx1.md xx2.md -o test.docx String command; File mubanDoc = new File(reference_docx); if (mubanDoc.exists()) { System.out.println("使用docx模板进行文档转换: " + reference_docx); command = PANDOC_PATH + " " + mdFilePath + " -o " + docxPath + " --toc-depth=3 --reference-doc=" + reference_docx; } else { //pandoc xx1.md xx2.md -o test.docx command = PANDOC_PATH + " " + mdFilePath + " -o " + docxPath + " "; } System.out.println(command); Process exec = rn.exec(command); } catch (Exception e) { System.out.println("调用服务生成word文档错误 " + e.getMessage()); } } /** * 1 (pandoc test.md -o test.docx 无法识别../imgs ),将../imgs替换为/imgs绝对路径 * <p> * 2 修改 markdown文档中的图片引入方式,手动将文档中所有../imgs 替换为/imgs ![业务用例图](/imgs/complex-email.png) * <p> * 3 通过copyImageDir()方法,将/docs/imgs目录下的图片,复制到当前程序运行的根目录/的imgs下(/imgs) */ private static void copyImageDir() { //文档中依赖图片文件路径 File docImgsDir = new File(IMG_DIR); //解决pandoc 转换文件时,找不到图片路径../imgs问题 File dir = new File("/imgs"); if (!dir.exists()) { dir.mkdir(); File[] files = docImgsDir.listFiles(); for (File file : files) { String s = FileUtil.readToString(file); try { FileUtil.writeToFile(dir + "" + file.getName(), s, "utf-8", false); } catch (IOException e) { e.printStackTrace(); } } System.out.println("复制文档图片到路径:" + dir.getAbsolutePath()); } else { System.out.println("文档图片已存在路径:" + dir.getAbsolutePath()); } }}

参考文档

Pandoc使用技巧: https://blog.csdn.net/weixin_39617497/article/details/117897721

pandoc使用latex模板转pdf文件: https://blog.51cto.com/u_1472521/5202317

使用 pandoc 进行文档转换(markdown转word)

使用screw生成数据库文档

文档地址: 「链接」 https://gitee.com/leshalv/screw

生成文档示例

声明:本文内容仅代表作者个人观点,与本站立场无关。如有内容侵犯您的合法权益,请及时与我们联系,我们将第一时间安排处理

相关推荐
热门精选
返回首页版权声明网站地图返回顶部

本站为非赢利性站点,为书友提供一个分享与交流的平台。本站所收录的作品、社区话题、用户评论、用户上传内容或图片等均属用户个人行为。如前述内容侵害您的权益,欢迎举报投诉,一经核实,立即删除,本站不承担任何责任

菜科网-日常生活百科知识大全,是大家的选择!

鄂ICP备17021050号-10