java使用pandoc将markdown转换为word文档
简介:大纲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 「链接」
需要通过右键修改样式方式,模板才能生效
markdown转word样式没处理好,生成word后新建样式手动处理
通过名称可以控制样式排序
段落背景色设置
前后缩进控制
java 操作pandoc 批量转换为word文档效果图预览:
markdown文档
转换后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
生成文档示例
声明:本文内容仅代表作者个人观点,与本站立场无关。如有内容侵犯您的合法权益,请及时与我们联系,我们将第一时间安排处理
- 米兰后卫佳夫谈伊布、更衣室和成长:与伊布踢球疯狂,大教堂是我的
米兰后卫佳夫近日接受了德国天空体育记者的采访,谈到了自己在米兰的生活、加盟前与马尔蒂尼和马萨拉的视频通...
体育新闻 2023-05-08 01:02:36 - 皇马成为首支达成百冠的西班牙俱乐部,历史冠军分布展现辉煌历程
在2023年5月7日的国王杯决赛中,皇马以2-1的比分击败了奥萨苏纳,成功捧起了这一项西班牙足球比赛中的冠军奖杯。...
体育新闻 2023-05-07 10:47:13 - 米兰前锋莱奥遭意大利媒体记者极具争议的言论:甚至让他断腿!莱奥
最近,一则关于意甲球员莱奥的争议言论引发了广泛关注。意大利媒体sportitalia的记者Michele Criscitiello在一档节目中,...
体育新闻 2023-04-28 00:03:14 - 布斯克茨将接受巴萨续约报价,薪水大幅减少适应财务公平竞争?
据报道,巴塞罗那队的中场球员布斯克茨打算接受巴萨提出的续约报价,以继续留在球队一个赛季,并有可能再加一...
体育新闻 2023-04-27 21:55:43 - FIFA23公布英超最佳前锋阵容名单,曼城球员入选最多
英超、FIFA23、最佳阵候选、前锋、阿尔米隆、马丁内利、格拉利什、哈兰德、哈弗茨、凯恩、马赫雷斯、努涅斯、拉...
体育新闻 2023-04-18 06:40:54 - 海外票务网站公布首轮G1部分场次平均票价,国王主场迎战勇士最高
NBA季后赛将在4月17日正式开始,各支球队和球迷们都在紧张备战和期待中。而海外票务网站也在今天公布了首轮G1部分...
体育新闻 2023-04-15 05:10:48 - 曼联与诺丁汉森林交手记录 5场比赛
两支球队也将在2022年12月28日凌晨4点进行2022赛季的英超联赛,第17轮比赛,目前两支球队的近期状况,曼联会略高一...
体育新闻 2023-03-24 21:31:22 - 大概3000的预算,买switch还是ps4?
如果你是学生党,优先建议switch。为什么?现在国内switch群体庞大,数千万的switch玩家基数,总有一款适合你。swit...
体育新闻 2022-12-23 21:00:36 - 队友变对手,同效力大巴黎的姆巴佩与梅西迎来巅峰对决 冠军决赛)
梅西和姆巴佩都效力于大巴黎俱乐部,这两支顶级的足球队员在足球联赛中属于队友,但梅西和姆巴佩隶属于不同的...
体育新闻 2022-12-19 08:36:45 - 你认为老子《道德经》的精髓是什么?你是如何评价这部作品的?
世间要有道!人类要守自然和谐之道!道,无,发万物生死在其中,老子仿之,修掉有的负担,仿道的生,也就是修...
体育新闻 2022-12-18 13:12:34