Reputation: 8454
With the script below Im trying to create a word doc. with one box for each Name and be exactly like in the image. Included inside a box with 2 cm width and 8 cm height.Top is the Date then blue line then school in red then Name in black,then logo and then role in blue. There should be 64 cards (one card for each participant), 3 or 4 cards in each MS word row.
# Sample Test_cards dataset (should contain 64 rows in your actual data)
Test_cards <- tibble::tibble(
ID = 1:64,
Name = paste0("Name_", 1:64),
Role = rep(c("Radiation Officer", "Radiologist", "Senior Manager", "Senior Technologist", "Technologist"), length.out = 64),
Date = rep("Q3-2024", 64),
School = rep("World’s College Hospital Here (Radiology)", 64),
Logo = NA # Assuming the same logo for simplicity
)
# Load necessary libraries
# Load necessary libraries
library(readxl)
library(officer) # For creating Word documents
library(flextable) # For advanced table layout and styling
library(magick) # For image handling
library(tibble) # For tibble data structure
# Path to logo image (ensure the correct path)
logo_path <- "logo.png"
# Define the custom styling with fp_text() for school name (red) and other fields
school_fp <- fp_text(color = "red", bold = TRUE)
date_fp <- fp_text(color = "black", bold = TRUE)
name_fp <- fp_text(color = "black", bold = TRUE)
role_fp <- fp_text(color = "blue", bold = TRUE)
# Function to create a box for each entry with correct colors and formatting
create_box <- function(date, school, name, role, logo_path) {
# Create a flextable for the card content
box_table <- flextable(data.frame(Card = rep(NA, 5))) %>%
# Date (first row) in black, bold, centered
compose(i = 1, j = 1, value = as_paragraph(as_chunk(date, props = date_fp))) %>%
# Add thicker blue horizontal line after the date
hline(i = 1, j = 1, border = fp_border(color = "blue", width = 3)) %>%
# School (second row) in red, bold, centered
compose(i = 2, j = 1, value = as_paragraph(as_chunk(school, props = school_fp))) %>%
# Name (third row) in black, bold, centered
compose(i = 3, j = 1, value = as_paragraph(as_chunk(name, props = name_fp))) %>%
# Insert the logo in the fourth row
compose(i = 4, j = 1, value = as_paragraph(as_image(src = logo_path, width = 1.3, height = 1.5))) %>%
# Role (fifth row) in blue, bold, centered
compose(i = 5, j = 1, value = as_paragraph(as_chunk(role, props = role_fp))) %>%
# Align all content to the center
align(align = "center", part = "all")
# Set the column width to 2.5 cm and adjust row heights for a total height of 5 cm
box_table <- width(box_table, j = 1, width = 2.5) # Set the width to 2.5 cm
box_table <- height_all(box_table, height = 1) # Adjust each row's height
# Add black borders around the table (box)
box_table <- border_outer(box_table, border = fp_border(color = "black", width = 2))
return(box_table)
}
# Create a new Word document
doc <- read_docx()
# Loop over each row in Test_cards and generate a card for each entry
for (i in seq_len(nrow(Test_cards))) {
# Extract the data for the current row
date <- Test_cards$Date[i]
school <- Test_cards$School[i]
name <- Test_cards$Name[i]
role <- Test_cards$Role[i]
# Create the card (box) for this row
box <- create_box(date, school, name, role, logo_path)
# Add the box to the document
doc <- body_add_flextable(doc, value = box)
# Add a page break after each card except the last one
if (i < nrow(Test_cards)) {
doc <- body_add_break(doc)
}
}
# Save the document
print(doc, target = "Test_cards_output.docx")
the logo
Upvotes: 0
Views: 30