【问题标题】:How to add hyperlinks to word docx using officeR?如何使用officeR将超链接添加到word docx?
【发布时间】:2017-10-30 13:01:13
【问题描述】:

如何使用 officeR 添加超链接?在 ReporteRs 包中有一个带有超链接参数的 POT;但我在 OfficeR 包中没有看到任何远程关闭的东西。

【问题讨论】:

    标签: r officer


    【解决方案1】:

    您找到解决问题的方法了吗?我写了一个函数来添加文档的超链接。但是,您必须在模板 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)
    }
    

    可能有更好的解决方案,但它对我有用。

    【讨论】:

      【解决方案2】:

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

      https://davidgohel.github.io/officer/reference/ph_hyperlink.html

      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"
      

      【讨论】:

        【解决方案3】:

        我能够使用以下代码在 docx 中添加超链接(请参阅 https://rdrr.io/cran/officer/man/slip_in_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"))
        
        

        【讨论】:

          猜你喜欢
          • 2021-06-25
          • 1970-01-01
          • 1970-01-01
          • 2019-12-08
          • 1970-01-01
          • 1970-01-01
          • 2014-01-10
          • 2013-01-01
          相关资源
          最近更新 更多