如何通过编程方式在officedown文档中从0开始页码编号?
问题:编程修改officedown生成docx的起始页码为0
渲染以下R Markdown代码会生成一个包含两页的docx文档,页码显示为1和2。我希望将页码改为0和1,是否可以通过编程方式实现该需求?即无需修改渲染时使用的reference_docx。
--- output: officedown::rdocx_document --- ```{r setup, include=FALSE} library(officedown) # 0.3.0
A simple header on page 1
\newpage
A simple header on page 2
## 解决方案 可以通过`officedown`结合`officer`包的功能,在文档渲染后直接修改页码起始值,完全不需要改动参考文档。具体实现代码如下: ```yaml --- output: officedown::rdocx_document --- ```{r setup, include=FALSE} library(officedown) # 0.3.0 library(officer)
# 获取当前正在构建的docx文档对象 current_doc <- docx_current() # 定义节属性:设置页码从0开始,使用阿拉伯数字 section_prop <- prop_section( page_numbering = list( start_at = 0, num_type = "arabic" ) ) # 将节属性应用到文档,让第一页就以0作为起始页码 current_doc <- body_end_section(current_doc, block_section(section_prop))
A simple header on page 1
\newpage
A simple header on page 2
### 关键说明 - `prop_section()`用来定义文档节的页码规则,`start_at`参数直接指定起始页码为0 - `docx_current()`获取当前正在生成的docx对象,确保修改作用于目标文档 - 整个过程完全通过代码控制,不需要额外修改reference_docx文件 内容的提问来源于stack exchange,提问作者Patrick
相关产品推荐
相关产品推荐

