2017-06-22 20:40:02
在R中,几乎没有完美的Office自动化方案,
所以——能不用Office就别用。
library(openxlsx) df <- data.frame( "Date" = Sys.Date()-0:19, "LogicalT" = TRUE, "Time" = Sys.time()-0:19*60*60, "Cash" = paste("$",1:20), "Cash2" = 31:50, "hLink" = "https://CRAN.R-project.org/", "Percentage" = seq(0, 1, length.out=20), "TinyNumbers" = runif(20) / 1E9, stringsAsFactors = FALSE) class(df$Cash) <- "currency" class(df$Cash2) <- "accounting" class(df$hLink) <- "hyperlink" class(df$Percentage) <- "percentage" class(df$TinyNumbers) <- "scientific" hs <- createStyle(fontColour = "#ffffff", fgFill = "deepskyblue", halign = "center", valign = "center", textDecoration = "Bold", border = "TopBottomLeftRight") write.xlsx(df, file = "writeXLSXTable3.xlsx", asTable = TRUE, borders = "rows", headerStyle = hs)
read_docx
styles_info
body_add_par
body_add_img
body_add_table
body_add_break
body_add_toc
body_add_gg
slip_in_img
slip_in_seqfield
slip_in_text
slip_in_tableref
slip_in_plotref
cursor_begin
cursor_end
cursor_reach
cursor_backward
cursor_forward
cursor_bookmark
body_remove
body_end_section
body_bookmark
docx_summary
library(officer) library(ggplot2) library(magrittr) gg <- ggplot(iris, aes( Sepal.Length, Petal.Length)) + geom_point() doc <- read_docx() %>% body_add_toc() %>% body_add_par("Example Doc", style = "heading 1") %>% rvg::body_add_vg(print(gg)) %>% body_add_par("Hello world!", style = "Normal") %>% body_add_par("", style = "Normal") %>% body_add_table(head(iris), style = "table_template") %>% body_add_par("", style = "Normal") %>% body_add_table(head(iris), style = "Table Professional") print(doc, target = "first_example.docx")
rvb::body_add_vg: 添加可编辑图
read_pptx
layout_sumamry
add_slide
remove_slide
on_slide
ph_with_text
ph_add_text
ph_add_par
/ ph_add_fpar
ph_with_image
ph_with_image_at
ph_with_table
ph_with_table_at
slide_summary
ph_hyperlink
ph_slidelink
ph_remove
pres <- read_pptx() %>% add_slide(layout = "Two Content", master = "Office Theme") %>% ph_with_text(type = "body", str = "A first text", index = 1) %>% ph_add_par(id_chr='2', level = 2) %>% ph_add_text("body (index 1) is text") %>% ph_with_text(type = "body", str = "A second text", index = 2) %>% ph_with_img_at( src = file.path(Sys.getenv("R_HOME"), "doc", "html", "logo.jpg"), left = 5.15, top = 2.5, height = 1.06, width = 1.39) %>% ph_with_text(type = "title", str = "A title") %>% ph_with_text(type = "ftr", str = "Slide footer") %>% ph_with_text(type = "dt", str = format(Sys.Date())) %>% add_slide(layout = "Title and Content", master = "Office Theme") %>% ph_with_text(type = "title", str = "mtcars") %>% ph_with_table(type = "body", value = head(mtcars)) print(pres, target = "ph_with_table.pptx")
library(flextable) library(data.table) dat <- dcast(data.table(mtcars), am + vs + cyl ~., list(length, mean, sd), value.var="mpg") ft1 <- flextable( data = as.data.frame(dat)) %>% theme_zebra() ft2 <- flextable( data = as.data.frame(dat)) %>% theme_booktabs() %>% set_header_labels( mpg_length = "#", mpg_mean = "Mean", mpg_sd = "SD") %>% color(i = ~ mpg_length > 3, color = "red") %>% autofit() read_docx() %>% body_add_par(value="Theme Zebra", style="table title") %>% body_add_flextable(ft1) %>% body_add_par("") %>% body_add_par(value="Theme booktabs", style="table title") %>% body_add_flextable(ft2) %>% print(target = "flextable.docx")
ppt = COMCreate("Powerpoint.Application")
comObj$methodName(arg1, arg2, arg3, ...)
comObj[['comPropertyName']]
library(RDCOMClient) # 初始化,创建COM对象,套模板 ppt <- COMCreate("Powerpoint.Application") ppt[['Visible']] <- TRUE slide <- ppt[['Presentations']]$add() slide$ApplyTheme("C:\\Program Files (x86)\\Microsoft Office\\Document Themes 14\\Urban.thmx") # 添加页面,标题和副标题 slide[['Slides']]$add(1, 1) x <- slide[['Slides']][[1]][['Shapes']][[1]][['TextFrame']][['TextRange']] x[['Text']] <- "Presentation" x <- slide[['Slides']][[1]][['Shapes']][[2]][['TextFrame']][['TextRange']] x[['Text']] <- "Sub-title" # 第二页,标题 slide[['Slides']]$add(2, 5) x <- slide[['Slides']][[2]][['Shapes']][[1]][['TextFrame']][['TextRange']] x[['Text']] <- "Title" # 第二页,左栏,四个列表 x <- slide[['Slides']][[2]][['Shapes']][[2]][['TextFrame']][['TextRange']] x[['Text']] <- paste(paste("Point", 1:4), collapse="\r") # 最后一个列表缩进一档 x <- slide[['Slides']][[2]][['Shapes']][[2]][['TextFrame']][['TextRange']]$Paragraphs(4) x[['IndentLevel']] <- 2
# 第二页右栏,新建图 x <- slide[['Slides']][[2]][['Shapes'] ]$AddChart(4)[['Chart']] #xlLine # 操作内嵌数据表,修改数据 gCd <- x[['ChartData']] gWb <- gCd[['Workbook']] gWs <- gWb[['Worksheets']][[1]] xlr <- gWs$Range("C1:G5") xlr$ClearContents() gWs$ListObjects("Table1")$Resize(gWs$Range("A1:B5")) xv <- gWs$Range("Table1[[#Headers],[Series 1]]") xv[['Value']] <- "Items" xv <- gWs$Range("a2"); xv[['Value']] = "Coffee" xv <- gWs$Range("a3"); xv[['Value']] = "Soda" xv <- gWs$Range("a4"); xv[['Value']] = "Tea" xv <- gWs$Range("a5"); xv[['Value']] = "Water" xv <- gWs$Range("b2"); xv[['Value']] = 1000 xv <- gWs$Range("b3"); xv[['Value']] = 2500 xv <- gWs$Range("b4"); xv[['Value']] = 4000 xv <- gWs$Range("b5"); xv[['Value']] = 3000 gCd$Activate() gWb$close(TRUE)
Thank you!