我正在下载一个 .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/