如何使用officeR添加超链接?在记者包中有一个带有超链接参数的罐子,但我在OfficeR包中没有看到任何东西。
发布于 2018-02-07 14:46:25
你找到解决问题的办法了吗?我编写了一个函数来将超链接添加到文档中。但是,您必须在模板docx文件中创建超链接的样式
add_hyperref = function (x, target="http://www.google.de",
style = NULL, pos = "after") {
if ( is.null(style) )
style <- x$default_styles$table
style_id <- x$doc_obj$get_style_id(style = style, type = "character")
refID = sprintf("rId%d",x$doc_obj$relationship()$get_next_id())
x$doc_obj$relationship()$add( refID,
type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink",
target=target, target_mode="External")
xml_elt = sprintf("<w:hyperlink r:id='%s' w:history='1'><w:r w:rsidRPr='00CD112F'><w:rPr><w:rStyle w:val='Hyperlink'/></w:rPr><w:t>LINK</w:t></w:r></w:hyperlink>",
refID)
xml_elt = paste0(officer:::wml_with_ns("w:p"), "<w:pPr><w:pStyle w:val=\"",
style_id, "\"/></w:pPr>", xml_elt, "</w:p>")
body_add_xml(x = x, str = xml_elt, pos = pos)
}也许有一个更好的解决方案,但它对我有效。
发布于 2019-04-12 15:25:26
@happ为Word文档提供了一个很好的解决方案(这可能是问题的根源),但我认为值得注意的是,有一个函数可以向PowerPoint文件添加超链接。
fileout <- tempfile(fileext = ".pptx")
doc <- read_pptx()
doc <- add_slide(doc, layout = "Title and Content", master = "Office Theme")
doc <- ph_with_text(x = doc, type = "title", str = "Un titre 1")
slide_summary(doc) # read column ph_label here
#> type id ph_label offx offy cx cy text
#> 1 title 2 Title 1 0.5 0.3003478 9 1.25 Un titre 1
doc <- ph_hyperlink(x = doc, ph_label = "Title 1",href = "https://cran.r-project.org")
print(doc, target = fileout )#> [1] "/private/var/folders/08/2qdvv0q95wn52xy6mxgj340r0000gn/T/RtmpXIYvn5/filef90c6579a4d2.pptx"发布于 2021-06-22 14:32:41
我能够使用以下代码在docx中添加一个超链接(请参阅text.html):
library(officer)
x <- read_docx()
x <- body_add_par(x, "Hello ", style = "Normal")
x <- slip_in_text(x, "world", style = "strong")
x <- slip_in_text(x, "Message is", style = "strong", pos = "before")
x <- slip_in_text(x, "with a link", style = "strong",
pos = "after", hyperlink = "https://davidgohel.github.io/officer/")
print(x, target = tempfile(fileext = ".docx"))https://stackoverflow.com/questions/47015951
复制相似问题