joshmire / officerWinTools

Functions to compliment officer R package using Microsoft Office in a Windows environment
6 stars 0 forks source link

print_docx_pdf cannot find the file #1

Open Pawek-13 opened 1 year ago

Pawek-13 commented 1 year ago

I have been tasked with creating a program that fills out a form with data from an Excel sheet. The desired output of this program is a filled out form saved as a .pdf file. I've tried using the print_docx_pdf function to do this, sadly, to no avail. When I saved the filled out form into a .docx file, using this line of code:

print(formularz, target = glue::glue("Formularz - {inputs$name}.docx"))

the form is saved with no issues (formularz is the result of the read_docx function and following code designed to automate filling out the form). However, when I try using print_docx_pdf, I get an error. My code looks like this:

print_docx_pdf(formularz, target = glue::glue("Formularz - {inputs$name}.pdf"))

and I get such an error message (this is a translation from Polish to English):

(C:\Users\Admin\Desktop\...\temp.docx)ularza\temp.vbs(4, 1) Microsoft Word: Sadly, the file cannot be found. Has it been moved has its name been changed?

What am I doing wrong that is causing this issue to happen? The name of the original file is Formularz_pusty.docx, if that is of any help.

joshmire commented 1 year ago

EDIT: Comment deleted as it did not understand the problem posed and would only serve to confuse future readers.

Pawek-13 commented 1 year ago

Sadly, the issue persists, with the same error message popping up as previously.

joshmire commented 1 year ago

My apologies. Your original code, print_docx_pdf(formularz, target = glue::glue("Formularz - {inputs$name}.pdf")), should have worked.

(C:\Users\Admin\Desktop\...\temp.docx)ularza\temp.vbs(4, 1) Microsoft Word: Sadly, the file cannot be found. Has it been moved has its name been changed?

If I'm interpreting the error correctly, it appears print_docx_pdf() is failing when executing commands found in a temp file that the function creates. Specifically, the output of paste('Set doc = objWord.Documents.Open("',normalizePath(paste(getwd(),"/temp.docx",sep="")),'")',sep="") in R appears to create a command that references a file that Microsoft Word cannot find. The location of the temp file referenced can be found by executing file.path(paste(getwd(),"/temp.vbs",sep="")) in R.

Three questions to move forward:

  1. Have you used the setwd() function to set your working directory before trying to use the print_docx_pdf() function?
  2. What is the output of paste('Set doc = objWord.Documents.Open("',normalizePath(paste(getwd(),"/temp.docx",sep="")),'")',sep="") when ran in place of the print_docx_pdf() function in your R script?
  3. What is the output of file.path(paste(getwd(),"/temp.vbs",sep="")) when ran in place of the print_docx_pdf() function in your R script?

Let me know if you're able to share the Excel file (or one with dummy data) and your R script. This (obviously) would make it easier to diagnose the error.

Pawek-13 commented 1 year ago

Answer 1: Yes.

Regarding questions 2 and 3: Am I supposed to run print_docx_pdf() beforehand or run your commands and completely omit print_docx_pdf()?

Regarding my code and other neccessary files, here they are. Here is my code:

# Załaduj funkcje ####

library(tidyverse)
library(readxl)
library(lubridate)
library(officer)
library(officerWinTools)

# Wybieranie odpowiedniego foldera ####

setwd("C:/Users/andrzej/Desktop/Wypełnianie_formularza")

# Załaduj dane ####

data <- read_excel("Klienci na zaprawianie 2023.xlsx", sheet = "Klienci")

# Funkcja dodająca kolumnę "Status VAT" ####

data <- data %>% mutate("Status VAT" = case_when(
  `Płatnik VAT` == "1" ~ "Płatnik VAT",
  `Rolnik Ryczałtowy / osoba fizyczna` == "1" ~ "Rolnik Ryczałtowy/ osoba fizyczna",
  T ~ "Inne"))

# Funkcja dodająca kolumnę "NIP/PESEL" ####

#data <- data %>% mutate("NIP/PESEL" = if_else(NIP == "", PESEL, NIP))

# Funcja dodająca kolumnę "Adres" ####

data <- data %>% unite(prawie_adres,
                       c("Ulica/Miejscowość", "Kpocz"), sep = ", ",
                       na.rm = TRUE) %>%
  unite(Adres, c("prawie_adres", "Poczta"), sep = " ")

# Nowa funkcja dla zawartości do uzupełnienia ####

form_inputs <- function(name, address, id_nip, id_pesel, vat_status, telephone_number,
                        email_ad, field_area, basic_order, basic_plus) {

  name <- name
  address <- address
  id_nip <- id_nip
  id_pesel <- id_pesel
  vat_status <- vat_status
  telephone_number <- telephone_number
  email_ad <- email_ad
  field_area <- field_area
  basic_order <- basic_order
  basic_plus <- basic_plus

  # utworzenie tabeli z informacjami do uzupełnienia

  df <- tibble(name = name,
    address = address,
    id_nip = id_nip,
    id_pesel = id_pesel,
    vat_status = vat_status,
    telephone_number = telephone_number,
    email_ad = email_ad,
    field_area = field_area,
    basic_order = basic_order,
    basic_plus = basic_plus)

  return(df)
}

# Nowa funkcja umieszczająca wartości do uzupełnienia ####

formfunction <- function(forminputs) {

  #wczytaj plik z formularzem

  df <- read_docx("Formularz_pusty.docx")

  #funkcje do odczytania i zastąpienia tekstu

  df <- body_replace_all_text(df,
                              old_value = "name",
                              new_value = inputs$name)

  df <- body_replace_all_text(df,
                              old_value = "address",
                              new_value = inputs$address)

  df <- body_replace_all_text(df,
                              old_value = "id_nip",
                              new_value = as.character(inputs$id_nip))

  df <- body_replace_all_text(df,
                              old_value = "id_pesel",
                              new_value = as.character(inputs$id_pesel))

  df <- body_replace_all_text(df,
                              old_value = "vat_status",
                              new_value = inputs$vat_status)

  df <- body_replace_all_text(df,
                              old_value = "telephone_number",
                              new_value = as.character(inputs$telephone_number))

  df <- body_replace_all_text(df,
                              old_value = "email_ad",
                              new_value = inputs$email_ad)

  df <- body_replace_all_text(df,
                              old_value = "field_area",
                              new_value = as.character(inputs$field_area))

  df <- body_replace_all_text(df,
                              old_value = "basic_order",
                              new_value = as.character(inputs$basic_order))

  df <- body_replace_all_text(df,
                              old_value = "basic_plus",
                              new_value = as.character(inputs$basic_plus))

  df <- body_replace_all_text(df,
                              old_value = "NA",
                              new_value = "")

  return(df)
}

# Pętla do wypełniania formularzy ####

i <- 1

for (i in 1:nrow(data)) {

  # sprawdzenie, czy utworzyć formularz

  if (!is.na(data$`Formularz @`[i])) {

    # dane do wypełnienia formularza

    inputs <- form_inputs(
      name = data$`Nazwa kontrahenta`[i],
      address = data$Adres[i],
      id_nip = data$NIP[i],
      id_pesel = data$PESEL[i],
      vat_status = data$`Status VAT`[i],
      telephone_number = data$`Tel. 1`[i],
      email_ad = data$`Email 1`[i],
      field_area = data$Hektary[i],
      basic_order = data$`Pakiet Podstawowy w T`[i],
      basic_plus = data$`Pakiet SYSTIVA w T`[i])

    # wypełnienie formularza

    formularz <- formfunction()

    # zapisanie wypełnionego formularza w nowym pliku

    setwd("C:/Users/andrzej/Desktop/Wypełnianie_formularza/Formularze")

    print_docx_pdf(formularz,
          target = glue::glue("Formularz - {inputs$name}.pdf"))

    setwd("C:/Users/andrzej/Desktop/Wypełnianie_formularza")

  }

  i <- i+1
}

Here is the form and some dummy data to fill it out. My program saves the filled-out forms in a directory called Formularze, soo create it as well to fully emulate how my code functions. Formularz_pusty.docx Klienci na zaprawianie 2023.xlsx

Pawek-13 commented 1 year ago

@joshmire Were you able to look at my code?