使用officer创建的word文档中的页码

发布于 2025-01-11 19:59:45 字数 1143 浏览 0 评论 0原文

与官员创建文档时,我使用 body_end_section_portrait() 和 body_end_section_landscape() 来设置方向。

library(officer)

doc <- officer::read_docx()

doc <- officer::body_add_par(doc, "bla1", style = "Normal")
doc <- officer::body_end_section_portrait(doc)

doc <- officer::body_add_par(doc, "bla2", style = "Normal")
doc <- officer::body_end_section_landscape(doc)

doc <- officer::body_add_par(doc, "bla3", style = "Normal")
doc <- officer::body_end_section_portrait(doc)
print(doc, target = "bb.docx")

当我将页码放入创建的文档中时,它们的顺序不正确。例如,在提供的示例中,创建的文档中的数字为 1、3、5、6。

我做错了什么?

> sessionInfo()
R version 4.1.2 (2021-11-01)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 22000)

Matrix products: default

locale:
[1] LC_COLLATE=Croatian_Croatia.1250  LC_CTYPE=Croatian_Croatia.1250   
[3] LC_MONETARY=Croatian_Croatia.1250 LC_NUMERIC=C                     
[5] LC_TIME=Croatian_Croatia.1250    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] officer_0.4.1 rJava_1.0-6  

when creating documents with officer I am using body_end_section_portrait() and body_end_section_landscape() to set up orientations.

library(officer)

doc <- officer::read_docx()

doc <- officer::body_add_par(doc, "bla1", style = "Normal")
doc <- officer::body_end_section_portrait(doc)

doc <- officer::body_add_par(doc, "bla2", style = "Normal")
doc <- officer::body_end_section_landscape(doc)

doc <- officer::body_add_par(doc, "bla3", style = "Normal")
doc <- officer::body_end_section_portrait(doc)
print(doc, target = "bb.docx")

When I put page numbers in the created document they are not in the right order. E.g. in the provided example the numbers in the created document go 1, 3, 5, 6.

What am I doing wrong?

> sessionInfo()
R version 4.1.2 (2021-11-01)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 22000)

Matrix products: default

locale:
[1] LC_COLLATE=Croatian_Croatia.1250  LC_CTYPE=Croatian_Croatia.1250   
[3] LC_MONETARY=Croatian_Croatia.1250 LC_NUMERIC=C                     
[5] LC_TIME=Croatian_Croatia.1250    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] officer_0.4.1 rJava_1.0-6  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陪你到最终 2025-01-18 19:59:45

好吧,似乎要走的路是使用 body_end_block_section() ,您可以在其中定义分节符类型。

首先,我定义了我的部分的外观

#landscape section
landscape=block_section(
  prop_section(
    page_size = page_size(orient="landscape"), type = "nextPage"
  )
)

#portrait section
portrait=block_section(
  prop_section(
    page_size = page_size(orient="portrait"), type = "nextPage"
  )
)

现在的代码是(我用 body_end_block_section() 更改了 body_end_section_landscape()/portrait() ):

doc <- officer::read_docx()
doc <- officer::body_add_par(doc, "bla1", style = "Normal")
doc <- officer::body_end_block_section(doc, value=portrait)

doc <- officer::body_add_par(doc, "bla2", style = "Normal")
doc <- officer::body_end_block_section(doc, value=landscape)

doc <- officer::body_add_par(doc, "bla3", style = "Normal")
doc <- officer::body_end_block_section(doc, value=portrait)
print(doc, target = "bb.docx")

Ok, it seems that the way to go is by using body_end_block_section() where you can define the section break type.

First I defined how my sections will look like

#landscape section
landscape=block_section(
  prop_section(
    page_size = page_size(orient="landscape"), type = "nextPage"
  )
)

#portrait section
portrait=block_section(
  prop_section(
    page_size = page_size(orient="portrait"), type = "nextPage"
  )
)

Now the code is (I changed body_end_section_landscape()/portrait() with body_end_block_section()):

doc <- officer::read_docx()
doc <- officer::body_add_par(doc, "bla1", style = "Normal")
doc <- officer::body_end_block_section(doc, value=portrait)

doc <- officer::body_add_par(doc, "bla2", style = "Normal")
doc <- officer::body_end_block_section(doc, value=landscape)

doc <- officer::body_add_par(doc, "bla3", style = "Normal")
doc <- officer::body_end_block_section(doc, value=portrait)
print(doc, target = "bb.docx")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文