我如何保持标题&在 R 中使用 pandoc 将 .docx 转换为 .md 时的副标题? [英] How do I keep title & subtitle when using pandoc to convert .docx to .md in R?

查看:81
本文介绍了我如何保持标题&在 R 中使用 pandoc 将 .docx 转换为 .md 时的副标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在下载 .docx 格式的 Google 文档,然后转换为 Markdown 以进行操作并导出为多种格式.

I'm downloading a Google Doc as .docx and then converting to markdown for manipulation and export to multiple formats.

问题:当我使用 pandoc 进行转换时,它会去除标题(和副标题)并且不添加任何 YAML 标头信息.我可以在标题中手动添加标题,但我需要编写脚本,因此不需要丢失标题(理想情况下)或从 docx 中提取标题并添加到 YAML 标题中,然后将其连接到转换后的降价文件.

Problem: When I convert using pandoc, it strips title (and subtitle) and does not add any YAML header information. I could add title manually in the header, but I need it to be scripted, so need to not lose the title (ideally) or extract title from docx and add to YAML header, which would then be concatenated to the converted markdown file.

示例代码,其中标题在从 docx 转换为 Markdown 时丢失:

Example Code, where title is lost on conversion from docx to markdown:

require(rmarkdown);require(devtools)
examplefile=paste0(tempdir(),"/example.docx")
download.file("https://file-examples.com/wp-content/uploads/2017/02/file-sample_100kB.docx",destfile=examplefile)
pandoc_convert(examplefile,to="markdown",output = "example.rmd", options=c("--extract-media=."))

render(paste0(tempdir(), "/example.rmd"),"html_document")
browseURL(paste0(tempdir(),"/example.html"))

推荐答案

当从 docx 转换为 Markdown(或其他标记格式,如 rst)时,您需要包含 -s--standalone 选项.

When converting from docx to markdown (or another markup format like rst) you need to include the -s or --standalone option.

来自 pandoc 文档:

From the pandoc documentation:

-s, --standalone

-s, --standalone

使用适当的页眉和页脚生成输出(例如独立的 HTML、LaTeX、TEI 或 RTF 文件,而不是片段).此选项是为 pdf、epub、epub3、fb2、docx 和 odt 输出自动设置的.对于本机输出,此选项会导致包含元数据;否则,元数据将被抑制.

Produce output with an appropriate header and footer (e.g. a standalone HTML, LaTeX, TEI, or RTF file, not a fragment). This option is set automatically for pdf, epub, epub3, fb2, docx, and odt output. For native output, this option causes metadata to be included; otherwise, metadata is suppressed.

如果没有 -s,此数据将被抑制.

Without the -s this data is suppressed.

这篇关于我如何保持标题&在 R 中使用 pandoc 将 .docx 转换为 .md 时的副标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆