如何将 R 的 Reticulate 包与 Python 的 OpenPyxl 一起使用以隐藏 excel 中的行

How to use R's Reticulate package alongside Python's OpenPyxl to hide rows in excel

我正在尝试创建一个 excel 文件,其中包含 hidden/folded 行,就像在 openpyxl webpage 上一样,但是通过 R 中的 reticulate 包。目前,我可以在这个过程中做所有事情,从生成虚拟数据到保存文件,除了操纵我的 sheet 的 row_dimensions。我走这条路是因为 none 的 R 的 excel 写作包,至少从我所发现的来看,能够折叠行。 下面的代码代表当前的形式,虽然它不改变行尺寸,但能够输出到工作 excel 文件。

rc1<-letters
rc2<-seq(1,1040,40)
rc3<-seq(0,18199,700)
rc<-data.frame(rc1,rc2,rc3)
library(reticulate)
pyxl<-import("openpyxl")
rct<-pyxl$Workbook()
pcta<-rct$active
pcta$title<-"Trial"
library(magrittr)
for (i in 1:length(rc1)) {
  for (j in 1:length(rc)) {
    a<-rc[i,j]
    a %>% pcta$cell(i,j,.)
  }
}
pcta$row_dimensions[1:4]<-10
rct$save("trial.xlsx")

我也试过只分配给单个值,然后当上面代码中的 1:41"1" 替换时导致跟随错误。

Error in py_call_impl(callable, dots$args, dots$keywords) : 
  TypeError: 'float' object is not iterable 
 Error in py_call_impl(callable, dots$args, dots$keywords) : 
  TypeError: '<' not supported between instances of 'str' and 'int' 

一个小时左右后才post回答我自己的问题有点尴尬,但我想我会post为了其他人的利益试图解决类似的问题。首先,需要在 as.integer() 内声明数字以避免浮点类型。其次,pcta$row_dimensions[]不是实现这个的方法,而是pcta$row_dimensions$group()。我已经制作了以下解决方案。

rc1<-letters
rc2<-seq(1,1040,40)
rc3<-seq(0,18199,700)
rc<-data.frame(rc1,rc2,rc3)
library(reticulate)
pyxl<-import("openpyxl")
rct<-pyxl$Workbook()
pcta<-rct$active
pcta$title<-"Trial"
library(magrittr)
for (i in 1:length(rc1)) {
  for (j in 1:length(rc)) {
    a<-rc[i,j]
    a %>% pcta$cell(i,j,.)
  }
}
pcta$row_dimensions$group(as.integer(1),as.integer(5),hidden = TRUE)
rct$save("trial.xlsx")