ycphs / openxlsx

openxlsx - a fast way to read and write complex xslx files
https://ycphs.github.io/openxlsx/
Other
225 stars 75 forks source link

write.xlsx(): [Content_Types.xml] file contains reference to /xl/drawings/drawing1.xml, but not the file, which creates problems with openpyxl #429

Open xjlc opened 1 year ago

xjlc commented 1 year ago

Describe the bug An Excel file written by openxlsx::write.xlsx() contained a reference (in [Content_Types.xml]) to a nonexistent file (/xl/drawings/drawing1.xml). When trying to import this file into PsychoPy (depending on openpyxl), an error was raised: KeyError: „There is no item named ‚xl/drawings/drawing1.xml‘ in the archive“

To Reproduce Not sure if this is generally reproducible. It is with my current setup. Here is the R code:

library(tidyverse)

designDir <- "."

base <- expand.grid(
  iV1=c("A", "B"),
  iV2=c("c", "ic"),
  list=1:2)

# compute different orders (nested: we want iV2 to vary within iV1)
for (i in 1:8) {
  # thisbase: list 1 for 1..4, list 2 for 5..8
  thisbase <- base %>% filter(base$list==base$list[i])
  switch( (i-1)%%4+1, # this evalutes to 1,2,3,4,1,2,3,4
    {blockdesign <- thisbase %>% arrange(iV1, iV2)},            #1
    {blockdesign <- thisbase %>% arrange(desc(iV1), iV2)},      #2
    {blockdesign <- thisbase %>% arrange(iV1, desc(iV2))},      #3
    {blockdesign <- thisbase %>% arrange(desc(iV1), desc(iV2))} #4
  )
  # for convenience, compute condsfile (to specify inner loop conditions file in PsychoPy)
  blockdesign <- blockdesign %>%
    mutate(condsfile = paste0("Exp4_", iV1, "_", iV2, "_", list, ".xlsx"))
  # write balance file
  blockfilename <- file.path(designDir, paste0("balance_", i, ".xlsx"))
  openxlsx::write.xlsx(blockdesign, file=blockfilename)
 }

Expected behavior I guess that either the referenced file drawing1.xml should be present or the reference to it should be absent.

I am a big fan of openxlsx and have never encountered any problems, but this one seems weird.

Example files balance_1.xlsx

Additional context I'm using version 4.2.5.2. The directory I'm writing the files to is under git control.

ScaonE commented 11 months ago

Having the same issue on my side. Tried the fixes suggested here without success.