r - 在R中使用pandoc将.docx转换为.md时如何保留标题和副标题?

标签 r r-markdown pandoc bookdown

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

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

示例代码,其中从 docx 转换为 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 选项。

来自 pandoc documentation :

-s, --standalone

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,该数据将被抑制。

关于r - 在R中使用pandoc将.docx转换为.md时如何保留标题和副标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59652509/

相关文章:

markdown - 使用 pandoc 右对齐 markdown 行的一部分

R Shiny 的绘图颜色有条件地基于单独数据框上的值

r - 在 Rmarkdown 的 YAML header 中设置数字的标题字体大小

RMarkdown Octave 图

pandoc - pandoc HTML 模板中的相对路径

r-markdown - 使用 R markdown 为每个页面创建页脚

r - 我的数据如何存储在 R 中?

r - 控制 R 中中止的函数内的工作目录

r - 阴谋无处

knitr - RMarkdown 中的多线方程