Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.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中,如何从一个字符串中提取特定行,该字符串从一个关键字开始,到另一个关键字结束?_Python_Regex_Python Imaging Library_Docx_Re - Fatal编程技术网

在python中,如何从一个字符串中提取特定行,该字符串从一个关键字开始,到另一个关键字结束?

在python中,如何从一个字符串中提取特定行,该字符串从一个关键字开始,到另一个关键字结束?,python,regex,python-imaging-library,docx,re,Python,Regex,Python Imaging Library,Docx,Re,我的代码的目标是能够从word文档中提取文本,并为每个有关键字的实例提取行,直到关联的零件号,例如: 处理器204执行以下一个或多个操作:通过传输中的组件检测另一组件已被移除244C,通过该组件检测 将成为: 通过运输中的一个部件检测到另一个部件已被拆下244C 除此之外,我还需要将该文本置于我用代码创建的图像的中心。这是我的密码: import re import time import textwrap from docx import Document from PIL import Im

我的代码的目标是能够从word文档中提取文本,并为每个有关键字的实例提取行,直到关联的零件号,例如:

处理器204执行以下一个或多个操作:通过传输中的组件检测另一组件已被移除244C,通过该组件检测

将成为:

通过运输中的一个部件检测到另一个部件已被拆下244C

除此之外,我还需要将该文本置于我用代码创建的图像的中心。这是我的密码:

import re
import time
import textwrap
from docx import Document
from PIL import Image, ImageFont, ImageDraw

doc = Document('PatentDocument.docx')
docText = ''.join(paragraph.text for paragraph in doc.paragraphs)
print(docText)

for i, p in enumerate(docText):
    W, H = 300, 300
    body = Image.new('RGB', (W, H), (255, 255, 255))
    border = Image.new('RGB', (W + 2, H + 2), (0, 0, 0))
    border.save('border.png')
    body.save('body.png')
    patent = Image.open('border.png')
    patent.paste(body, (1, 1))
    draw = ImageDraw.Draw(patent)
    font = ImageFont.load_default()

    current_h, pad = 60, 20
    keywords = ['responsive', 'detecting', 'providing', 'Responsive', 'Detecting', 'Providing']
    pattern = re.compile('|'.join(keywords))
    parts = re.findall("\d{1,3}[C]", docText)
    print(parts)
    for keywords in textwrap.wrap(docText, width=50):
        line = keywords.encode('utf-8')
        w, h = draw.textsize(line, font=font)
        draw.text(((W-w)/2, current_h), line, (0, 0, 0), font=font)
        current_h += h + pad

    patent.save(f'patent_{i+1}_{time.strftime("%Y%m%d%H%M%S")}.png')
我的代码目前所做的是打印word文档中的整个文本的字符串,并输出整个文本的图像500多次,这是字符串中的字符计数。以下是我的一个输出示例:

此输出重复500多次。 除此之外,这些将在运行窗口中获得输出:

[0054]处理器204执行以下一项或多项操作:通过传输中的组件检测另一个组件已被移除244C;通过该组件检测替换组件已被添加到传输246C;通过该组件向替换组件提供数据,其中,数据试图破坏替换组件248C的授权功能,并响应授权功能的非颠覆,允许组件使用替换组件249C的授权功能。 ['244C','246C','248C','249C']

除此之外,段落后面的数组也重复了500多次

这是我从中读取并转换为单个字符串的word文档:

[0054]处理器204执行以下一项或多项操作:通过传输中的组件检测另一个组件已被移除244C;通过该组件检测替换组件已被添加到传输246C;通过该组件向替换组件提供数据,其中,数据试图破坏替换组件248C的授权功能,并响应授权功能的非颠覆,允许组件使用替换组件249C的授权功能

我现在想知道如何从我制作的字符串中提取特定的行。输出应该是这样的--忽略方框和居中--我只想输出我给出的段落中的那些行:

这方面的一些伪代码如下所示:

for keyword in docText:
     print({keyword, part number})

我目前的实现是使用docx、PIL和re,尽管我很乐意使用任何可以实现我目标的东西。什么都行

所以,在得到外界的帮助后,我设法把一切都解决了。减去用于输出带有居中文本的图像的代码以及所有这些,这是解决我的主要问题的代码:

from docx import Document
from PIL import Image, ImageFont, ImageDraw

doc = Document('PatentDocument.docx')
docText = ''.join(paragraph.text for paragraph in doc.paragraphs)
print(docText)


def get(source, begin, end):
    try:
        start = source.index(len(begin)) + len(begin)
        finish = source.index(len(end), len(start))
        return source[start:finish]
    except ValueError:
        return ""


def create_regex(keywords=('responsive', 'providing', 'detecting')):
    re.compile('([Rr]esponsive|[Pp]oviding|[Dd]etecting).*?(\\d{1,3}C)')
    regex = (
        "("
        + "|".join((f"[{k[0].upper()}{k[0].lower()}]{k[1:]}" for k in keywords))
        + ")"
        + ".*?(\\d{1,3}C)"
    )
    return re.compile(regex)


def find_matches(text, keywords):
    return [m.group() for m in re.finditer(create_regex(keywords), text)]


for match in find_matches(
    text=docText, keywords=("responsive", "detecting", "providing")
):
    print(match)
因此,从源文档:

[0054]处理器204执行以下一项或多项操作:通过传输中的组件检测另一个组件已被移除244C;通过该组件检测替换组件已被添加到传输246C;通过该组件向替换组件提供数据,其中,数据试图破坏替换组件248C的授权功能,并响应授权功能的非颠覆,允许组件使用替换组件249C的授权功能

我得到以下输出:

[0054]处理器204执行以下一项或多项操作:通过传输中的组件检测另一个组件已被移除244C;通过该组件检测替换组件已被添加到传输246C;通过该组件向替换组件提供数据,其中,数据试图破坏替换组件248C的授权功能,并响应授权功能的非颠覆,允许组件使用替换组件249C的授权功能

通过运输中的一个部件检测到另一个部件已被拆下244C

通过部件检测在运输246C中添加了替换部件

通过组件向替换组件提供数据,其中数据试图破坏替换组件248C的授权功能

响应授权功能的非颠覆,允许组件使用替换组件249C的授权功能

后面紧跟关键字字符串的字符串之间没有空格,但为了便于阅读,我将它们分开。希望这能帮到别人