首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用officeR向word docx添加超链接?

如何使用officeR向word docx添加超链接?
EN

Stack Overflow用户
提问于 2017-10-30 13:01:13
回答 3查看 1.1K关注 0票数 1

如何使用officeR添加超链接?在记者包中有一个带有超链接参数的罐子,但我在OfficeR包中没有看到任何东西。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-02-07 14:46:25

你找到解决问题的办法了吗?我编写了一个函数来将超链接添加到文档中。但是,您必须在模板docx文件中创建超链接的样式

代码语言:javascript
复制
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)
}

也许有一个更好的解决方案,但它对我有效。

票数 2
EN

Stack Overflow用户

发布于 2019-04-12 15:25:26

@happ为Word文档提供了一个很好的解决方案(这可能是问题的根源),但我认为值得注意的是,有一个函数可以向PowerPoint文件添加超链接。

hyperlink.html

代码语言:javascript
复制
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"
票数 0
EN

Stack Overflow用户

发布于 2021-06-22 14:32:41

我能够使用以下代码在docx中添加一个超链接(请参阅text.html):

代码语言:javascript
复制
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"))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47015951

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档