flextable icon indicating copy to clipboard operation
flextable copied to clipboard

Support for russian image names for Word

Open erydit opened this issue 4 years ago • 4 comments
trafficstars

Hello! officer is an essential and great package. Not without several annoying flaws, though.

  1. Proper work with UTF-8 file names. When I try to save flextable with images in the cells, and images have cyrillic file names, the Word would fail to open it. It is not a big deal to rename all used files. However, proper support for non-latin file names wil benefit the package.
  2. Support for International System of Units. By some reason the whole rmarkdown infrastructure, and officer in particular, ignores the SI units. All sizes are specifies in inches, with no posiibility to use cm, mm, etc. That seems weird, considering that MS Office, has no problems with it.

erydit avatar May 17 '21 09:05 erydit

Thanks

It's better to follow the guidelines so that it's easier to help. Please follow them next time.

  1. I don't think the package can solve issues that R has on Windows (I assume you are on Windows). Would you be able to suggest a solution? Did you try the work R Core Team is doing to support UTF8 on Windows ?

  2. I am not planning to spend time on that in the next months

davidgohel avatar May 17 '21 10:05 davidgohel

Ok, sorry, I thought that the lack of non-latin filenames support is a known issue. Here is an example:

library(officer) # version: 0.3.18
library(flextable) # version: 0.6.5
library(magrittr)

pic <- file.path( R.home("doc"), "html", "logo.jpg" )

pic_en <- file.path(tempdir(), "picture.jpg")
file.copy(from = pic, to = pic_en)

pic_ru <- file.path(tempdir(), "картинка.jpg")
file.copy(from = pic, to = pic_ru)

flx_tbl <- data.frame(col1 = c("English file name", "Russian file name"), 
                      col2 = c(pic_en, pic_ru)) %>%
  flextable() %>%
  colformat_image(j = "col2", width = 1, height = 1)

read_docx() %>%
  body_add_par("Picture with english file name:") %>%
  body_add_img(src = pic_en, width = 1, height = 1) %>%
  body_add_par("") %>%
  body_add_par("Picture with russian file name:") %>%
  body_add_img(src = pic_ru, width = 1, height = 1) %>%
  body_add_par("") %>%
  body_add_flextable(flx_tbl) %>%
  print(file.path(tempdir(), "ImageTest.docx"))

file.path(tempdir(), "ImageTest.docx")

When I opens the file ImageTest.docx with the MS Word 2010 i get the error: "The file ImageTest cannot be opened because there are problems with the contents." and possibility to attempt to restore it. After opening the file looks like on screenshot. The error says: "The images that cannot be displayed currently”. Screen-2021-05-18_01-47-17 Here is the resulted docx: ImageTest.docx

my current sessionInfo():

R version 4.0.5 (2021-03-31)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Manjaro Linux

Matrix products: default
BLAS:   /usr/lib/libblas.so.3.9.1
LAPACK: /usr/lib/liblapack.so.3.9.1

locale:
 [1] LC_CTYPE=ru_RU.UTF-8       LC_NUMERIC=C               LC_TIME=ru_RU.UTF-8        LC_COLLATE=ru_RU.UTF-8    
 [5] LC_MONETARY=ru_RU.UTF-8    LC_MESSAGES=ru_RU.UTF-8    LC_PAPER=ru_RU.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=ru_RU.UTF-8 LC_IDENTIFICATION=C       

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

other attached packages:
[1] flextable_0.6.5 officer_0.3.18  magrittr_2.0.1 

On my work computer with Windows 7 64-bit i get the same behavior. Maybe that is the flextable`s issue...

erydit avatar May 17 '21 22:05 erydit

Thank you, as expected, providing reproducible example helps!

So the issue is not related to officer but only flextable.

This works as expected:

library(officer)
library(magrittr)

pic <- file.path( R.home("doc"), "html", "logo.jpg" )

pic_en <- file.path(tempdir(), "picture.jpg")
file.copy(from = pic, to = pic_en)

pic_ru <- file.path(tempdir(), "картинка.jpg")
file.copy(from = pic, to = pic_ru)


read_docx() %>%
  body_add_par("Picture with english file name:") %>%
  body_add_img(src = pic_en, width = 1, height = 1) %>%
  body_add_par("") %>%
  body_add_par("Picture with russian file name:") %>%
  body_add_img(src = pic_ru, width = 1, height = 1) %>%
  body_add_par("") %>%
  print(file.path(tempdir(), "ImageTest.docx")) 

This does not work when output is Word :

library(flextable)
library(magrittr)

pic_ru <- file.path(tempdir(), "картинка.jpg")
file.copy(from = file.path( R.home("doc"), "html", "logo.jpg" ), to = pic_ru)

flx_tbl <- data.frame(x = pic_ru) %>%
  flextable() %>%
  colformat_image(j = "x", width = 1, height = 1)

print(flx_tbl, preview = "docx")

So the issue is about flextable.

davidgohel avatar May 18 '21 06:05 davidgohel

Ok, sorry, I thought that the lack of non-latin filenames support is a known issue.

I don't understand. Could you explain?

my current sessionInfo():

You did truncate your sessionInfo()

davidgohel avatar May 18 '21 07:05 davidgohel

closed by https://github.com/davidgohel/officer/commit/c0944daf2e0a07697a75f7d537adf59545cc7122 where image treatments are happening now.

until it's on CRAN, you need to update with:

remotes::install_github("davidgohel/officer")
remotes::install_github("davidgohel/flextable")

davidgohel avatar Dec 31 '22 17:12 davidgohel

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue and link to this old issue if necessary.

github-actions[bot] avatar Jul 06 '23 05:07 github-actions[bot]