Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python-从microsoft word中提取文本_Python_Ms Word - Fatal编程技术网

python-从microsoft word中提取文本

python-从microsoft word中提取文本,python,ms-word,Python,Ms Word,我试图提取MS word文档()特定部分中的文本-下面的示例。基本上,我需要将带有标签--ASN1START和--ASN1STOP的所有文本写入一个不包含上述标签的文件 示例文本 -- ASN1START CounterCheck ::= SEQUENCE { rrc-TransactionIdentifier RRC-TransactionIdentifier, criticalExtensions

我试图提取MS word文档()特定部分中的文本-下面的示例。基本上,我需要将带有标签
--ASN1START
--ASN1STOP
的所有文本写入一个不包含上述标签的文件

示例文本

-- ASN1START

CounterCheck ::=            SEQUENCE {
    rrc-TransactionIdentifier           RRC-TransactionIdentifier,
    criticalExtensions                  CHOICE {
        c1                                  CHOICE {
            counterCheck-r8                     CounterCheck-r8-IEs,
            spare3 NULL, spare2 NULL, spare1 NULL
        },
        criticalExtensionsFuture            SEQUENCE {}
    }
}

CounterCheck-r8-IEs ::= SEQUENCE {
    drb-CountMSB-InfoList               DRB-CountMSB-InfoList,
    nonCriticalExtension                CounterCheck-v8a0-IEs               OPTIONAL
}

CounterCheck-v8a0-IEs ::= SEQUENCE {
    lateNonCriticalExtension            OCTET STRING                        OPTIONAL,
    nonCriticalExtension                CounterCheck-v1530-IEs              OPTIONAL
}

CounterCheck-v1530-IEs ::= SEQUENCE {
    drb-CountMSB-InfoListExt-r15        DRB-CountMSB-InfoListExt-r15        OPTIONAL,   -- Need ON
    nonCriticalExtension                SEQUENCE {}                         OPTIONAL
}

DRB-CountMSB-InfoList ::=       SEQUENCE (SIZE (1..maxDRB)) OF DRB-CountMSB-Info

DRB-CountMSB-InfoListExt-r15 ::=    SEQUENCE (SIZE (1..maxDRBExt-r15)) OF DRB-CountMSB-Info

DRB-CountMSB-Info ::=   SEQUENCE {
    drb-Identity                    DRB-Identity,
    countMSB-Uplink                 INTEGER(0..33554431),
    countMSB-Downlink               INTEGER(0..33554431)
}

-- ASN1STOP
我尝试过使用
docx

from docx import *
import re
import json

fileName = './data/36331-f80.docx'
document = Document(fileName)

startText = re.compile(r'-- ASN1START')

for para in document.paragraphs:
    # look for each paragraph
    text = para.text
    print(text)
    # if startText.match(para.text):
    #     print(text)

似乎上面提到的每一行标签都是一个段落。我需要有关仅提取标记内文本的帮助。

您可以尝试先将所有文档/段落文本读入单个字符串,然后使用
re.findall
查找目标标记之间的所有匹配文本:

text = ""
for para in document.paragraphs:
    text += para.text + "\n"

matches = re.findall(r'-- ASN1START\s*(.*?)\s*-- ASN1STOP', text, flags=re.DOTALL)

请注意,我们在正则表达式中使用点所有模式,以确保
*
可以匹配跨换行符出现的标记之间的内容。

使用
text.append(para.text)
获取错误:
实例'str'没有'append'memberpylint(没有成员)
我们如何将这些匹配写入文件?注意,在打印(匹配)中存在许多转义字符,如-
'UERadioPagingInformation-NB::=序列{\tcriticalExtensions\t\t\t\t\t选择{\t\tc1\t\t\t\t\t\t选择{\t\t\tueradiopaginformation-r13\t\t\tueradiopaginformation NB IEs,
这很可能正是Python的解释器向您显示的内容。如果写入文本文件,则
\t
例如将显示为选项卡。我面临的一个问题是,输出中不会捕获原始文本中的换行符,而是每个文本中的文本ag被捕获为一行,这导致了一些语法错误。我更新了示例文本以反映此问题。每行以
--
开头的部分都是注释。是否有办法将换行符保留为原始文本中的换行符?是的,在
--ASN1START(.*)--ASN1STOP