python - Word 表格中的文本

标签 python python-3.x

我每天都会收到一封带有 Word 文档的电子邮件。文档中的所有文本都存在于文档的表格中。我有数百个这样的Word文档(我每天都会收到一个)。我想使用python打开每个文档,复制我需要的文本,并将其粘贴到excel文档中。然而,我陷入了第一部分。我无法从word文档中提取文本。我正在尝试使用 python-docx 模块来提取文本,但我不知道如何从表中读取文本。

我修改了我正在阅读的Python入门书中的一个getText模块,但它似乎不起作用。我是否走在正确的轨道上?

import docx
fullText = []

def getText(filename):
    doc = docx.Document(filename)
    for table in doc.Tables:
        for row in table.Rows:
            for cell in row.Cells:
                  fullText.append(cell.text)
    return '\n'.join(fullText)

好的,看完this other question我意识到我实际上遇到了与我想象的不同的问题。我已进行更改并具有以下代码:

import docx
fullText = []

doc = docx.Document('c:\\btest\\January18.docx')
for table in doc.tables:
    for row in table.rows:
            for cell in row.cells:
                  fullText.append(cell.text)
'\n'.join(fullText)

print(fullText)

它正在打印:

['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']

问题是,Word 文档中的表格不是空白单元格,因此它们不应该返回空白。我做错了什么?

A sample input document is here

我正在尝试从该文档中提取某些文本行,并按照我想要的方式粘贴和格式化文本。但是,我什至无法访问word文档中的文本...

最佳答案

我能够解析sample doc并使用以下脚本将其保存到 Excel 文件:

import re
import pandas
import docx2txt

INPUT_FILE = 'jantest2.docx'
OUTPUT_FILE = 'jantest2.xlsx'

text = docx2txt.process(INPUT_FILE)
results = re.findall(r'(\d+-\d+)\n\n(.*)\n\n(.*)\n\n(.*)', text)
data = {'Case Number': [x[0] for x in results],
        'Report Date': [x[1] for x in results],
        'Address': [x[2] for x in results],
        'Statute Descripiton': [x[3] for x in results]}

data_frame = pandas.DataFrame(data=data)
writer = pandas.ExcelWriter(OUTPUT_FILE)
data_frame.to_excel(writer, 'Sheet1', index=False)
writer.save()

这是我在 Excel 文件中得到的内容:

enter image description here

关于python - Word 表格中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53926320/

相关文章:

python - 使用线程调用 Py_Finalize 时出现 AssertionError(仅限 3.X)

Python函数读取长度不起作用

python - 使用 Selenium 和 Python 禁用 Shockwave Flash 插件

Python 排序和比较嵌套字典

python - 在 django ajax_lookup 中配置 ajax 查找

php - 强制删除 phpmyadmin 和 PHP7.0 子进程错误

python - 如何能够在 QListWidgets 之间拖放项目并仍然能够使用拖放进行排序

python - 如何按列值 reshape Pandas 数据框?

python - 使用并发选项开始之前的任务状态

python - 在 Python 中获取 DictReader 标题的特殊情况