【问题标题】:officer: edit Word table官员:编辑Word表
【发布时间】:2018-11-28 09:12:46
【问题描述】:

我正在尝试使用“officer”编辑 Word 表的值。 很容易找到包含它的“段落”

library(officer)

doc = read_docx('template.docx')
doc = cursor_begin(doc)
doc = cursor_reach(doc,"Some text")
print(doc)

我得到一个看起来像这样的文档:

* Content at cursor location:
 row_id is_header cell_id                    text col_span row_span
1.1       1     FALSE       1                   D            1        1
1.5       2     FALSE       1                                1        1
1.9       3     FALSE       1             Some text          1        1
1.13      4     FALSE       1                                1        1
2.2       1     FALSE       2            More text           1        1

但是,然后呢?似乎没有任何方法可以直接更改此表的内容...

【问题讨论】:

    标签: officer


    【解决方案1】:

    您可以使用body_replace_all_text 完成该任务。

    library(officer)
    
    
    doc <- read_docx()
    doc <- body_add_table(doc, iris, style = "table_template")
    
    doc = cursor_reach(doc, "setosa")
    doc <- body_replace_all_text(doc, old_value = "setosa", 
      new_value = "coco", only_at_cursor = TRUE)
    print(doc, target = "test.docx")
    

    【讨论】:

    • 甜蜜!不理想,因为我希望将文本添加到空单元格,但我可以在其中添加一个占位符文本来替换它。这行得通,谢谢!
    • 取决于您真正想做的事情,可能会有更简单的解决方案。例如,您可以从这些数据中获取一个 data.frame(参见此处:davidgohel.github.io/officer/articles/…),然后将单词表替换为数据的修改版本。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-30
    • 2018-08-03
    • 1970-01-01
    相关资源
    最近更新 更多