如何使用R

时间:2018-05-20 21:48:42

标签: r docx extraction

任何人都知道他们可以推荐的任何东西,只能从.docx格式的文章中提取纯文本(最好用R)?

速度并不重要,我们甚至可以使用有一些API的网站上传和提取文件,但我一直找不到。我需要提取我要删除的摘要,引用,特别是图形和表格的介绍,方法,结果和结论 的感谢

4 个答案:

答案 0 :(得分:3)

您应该会发现其中一个软件包可以帮到您。

在一天结束时,现代Office文件格式(OpenXML)只是包含结构化XML内容的* .zip文件,因此如果您拥有结构良好的内容,那么您可能只想以这种方式打开它。我将从这里开始(http://officeopenxml.com/anatomyofOOXML.php)你也应该能够取消OpenXML SDK的指导(https://msdn.microsoft.com/en-us/library/office/bb448854.aspx

答案 1 :(得分:1)

对于像这样的任务,

Pandoc是一个很棒的解决方案。使用名为a.docx的文档,您将在命令行运行

pandoc -f docx -t markdown -o a.md a.docx

然后,您可以在R中使用正则表达式工具从新创建的a.md中提取所需内容,即文本。默认情况下,不会转换图像。

顺便说一句,Pandoc是RStudio的一部分,所以你可能已经拥有它了。

答案 2 :(得分:1)

您可以尝试使用readtext库:

library(readtext)
x <- readtext("/path/to/file/myfile.docx")
# x$text will contain the plain text in the file

变量x只包含没有任何格式的文本,因此如果需要提取一些信息,则需要执行字符串搜索。例如,对于您在评论中提到的文档,可以采用以下方法:

library(readtext)
doc.text <- readtext("test.docx")$text

# Split text into parts using new line character:
doc.parts <- strsplit(doc.text, "\n")[[1]]

# First line in the document- the name of the Journal
journal.name <- doc.parts[1]
journal.name
# [1] "International Journal of Science and Research (IJSR)"

# Similarly we can extract some other parts from a header
issn <-  doc.parts[2]
issue <- doc.parts[3]

# Search for the Abstract:
abstract.loc <- grep("Abstract:", doc.parts)[1]

# Search for the Keyword
Keywords.loc <- grep("Keywords:", doc.parts)[1]

# The text in between these 2 keywords will be abstract text:
abstract.text <- paste(doc.parts[abstract.loc:(Keywords.loc-1)], collapse=" ")

# Same way we can get Keywords text:
Background.loc <- Keywords.loc + grep("1\\.", doc.parts[-(1:Keywords.loc)])[1]
Keywords.text <- paste(doc.parts[Keywords.loc:(Background.loc-1)], collapse=" ")
Keywords.text
# [1] "Keywords: Nephronophtisis, NPHP1 deletion, NPHP4 mutations, Tunisian patients"

# Assuming that Methods is part 2
Methods.loc <- Background.loc + grep("2\\.", doc.parts[-(1:Background.loc)])[1]
Background.text <- paste(doc.parts[Background.loc:(Methods.loc-1)], collapse=" ")


# Assuming that Results is Part 3
Results.loc <- Methods.loc- + grep("3\\.", doc.parts[-(1:Methods.loc)])[1]
Methods.text <- paste(doc.parts[Methods.loc:(Results.loc-1)], collapse=" ")

# Similarly with other parts. For example for Acknowledgements section:
Ack.loc <- grep("Acknowledgements", doc.parts)[1]
Ref.loc <- grep("References", doc.parts)[1]
Ack.text <- paste(doc.parts[Ack.loc:(Ref.loc-1)], collapse=" ")
Ack.text
# [1] "6. Acknowledgements We are especially grateful to the study participants. 
# This study was supported by a grant from the Tunisian Ministry of Health and 
# Ministry of Higher Education ...

确切的方法取决于您需要搜索的所有文档的通用结构。例如,如果第一部分总是命名为&#34;背景&#34;你可以用这个词来搜索。但是,如果有时这可能是&#34;背景&#34;有时&#34;简介&#34;然后你可能想搜索&#34; 1。&#34;图案。

答案 3 :(得分:0)

您可以使用包officer执行此操作:

library(officer)
example_pptx <- system.file(package = "officer", "doc_examples/example.docx")
doc <- read_docx(example_pptx)
summary_paragraphs <- docx_summary(doc)
summary_paragraphs[summary_paragraphs$content_type %in% "paragraph", "text"]
#>  [1] "Title 1"                                                                
#>  [2] "Lorem ipsum dolor sit amet, consectetur adipiscing elit. "              
#>  [3] "Title 2"                                                                
#>  [4] "Quisque tristique "                                                     
#>  [5] "Augue nisi, et convallis "                                              
#>  [6] "Sapien mollis nec. "                                                    
#>  [7] "Sub title 1"                                                            
#>  [8] "Quisque tristique "                                                     
#>  [9] "Augue nisi, et convallis "                                              
#> [10] "Sapien mollis nec. "                                                    
#> [11] ""                                                                       
#> [12] "Phasellus nec nunc vitae nulla interdum volutpat eu ac massa. "         
#> [13] "Sub title 2"                                                            
#> [14] "Morbi rhoncus sapien sit amet leo eleifend, vel fermentum nisi mattis. "
#> [15] ""                                                                       
#> [16] ""                                                                       
#> [17] ""
相关问题