python - 获取 R 中 doc/docx 文件的字数

标签 python r ms-word

我有一个 doc/docx 文档流,我需要计算其中的字数。

到目前为止的过程是手动打开文档并记下 MS Word 本身提供的字数统计,我正在尝试使用 R 使其自动化。

这是我尝试过的:

library(textreadr)
library(stringr)
myDocx = read_docx(myDocxFile)
docText = str_c(myDocx , collapse = " ")
wordCount = str_count(test, "\\s+") + 1

不幸的是,wordCount 不是 MS Word 建议的。

例如,我注意到 MS Word 计算编号列表中的数字,而 textreadr 甚至不导入它们。

有解决办法吗?我也不介意在 Python 中尝试一些东西,尽管我在这方面的经验较少。

如有任何帮助,我们将不胜感激。

最佳答案

这应该可以使用 R 中的 tidytext 包来完成。

library(textreadr)
library(tidytext)
library(dplyr)

#read in word file without password protection
x <- read_docx(myDocxFile)
#convert string to dataframe
text_df <-tibble(line = 1:length(x),text = x)
#tokenize dataframe to isolate separate words
words_df <- text_df %>%
  unnest_tokens(word,text)
#calculate number of words in passage
word_count <- nrow(words_df)

关于python - 获取 R 中 doc/docx 文件的字数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58644947/

相关文章:

r - R 中带有子集的 For 循环

c# - 使用 DocumentFormat.OpenXml dll 读取 .Doc 文件

html - 如何有效地将数学方程式从 Microsoft Word 导出到 HTML

将MSWord文件读入R

Python TastyPie - 自定义管理器方法作为过滤器?

r - 如何在R中计算两个数据帧之间的Jaccard相似度

Python Django 1.6 在查看之前为每个请求执行函数

r - 缩放颜色渐变并超出限制

python - 找不到服务 "taskqueue"的 api 代理

python - 在 python 脚本中读取 tar 文件内容而不解压缩它