python " />

扫描二维码下载沐宇APP

沐宇

微信扫码使用沐宇小程序

沐宇

鎬庝箞鐢╬ython鎵归噺鎻愬彇word鎸囧畾鍐呭

扬州沐宇科技
2023-11-09 00:23:41
python

瑕佷娇鐢≒ython鎵归噺鎻愬彇Word涓殑鎸囧畾鍐呭锛屼綘鍙互浣跨敤Python-docx搴撴潵澶勭悊Word鏂囨。銆備笅闈㈡槸涓€涓ず渚嬩唬鐮侊紝鍙互甯姪浣犳彁鍙朩ord鏂囨。涓殑鎸囧畾鍐呭锛?/p>

from docx import Document

def extract_content_from_word(file_path, target_text):
    doc = Document(file_path)
    extracted_content = []

    for paragraph in doc.paragraphs:
        if target_text in paragraph.text:
            extracted_content.append(paragraph.text)

    return extracted_content

# 璋冪敤绀轰緥
file_path = 'path_to_your_word_document.docx'  # 鏇挎崲涓轰綘鐨刉ord鏂囨。璺緞
target_text = '鎸囧畾鍐呭'  # 鏇挎崲涓轰綘瑕佹彁鍙栫殑鎸囧畾鍐呭

extracted_content = extract_content_from_word(file_path, target_text)
for content in extracted_content:
    print(content)

杩欐浠g爜浣跨敤Python-docx搴撴墦寮€鎸囧畾璺緞涓嬬殑Word鏂囨。锛屽苟閬嶅巻鏂囨。鐨勬瘡涓钀姐€傚鏋滄钀戒腑鍖呭惈鐩爣鏂囨湰锛屽氨灏嗚娈佃惤鍐呭娣诲姞鍒?code>extracted_content鍒楄〃涓€傛渶鍚庯紝鎵撳嵃鎻愬彇鍒扮殑鍐呭銆?/p>

璇锋浛鎹?code>file_path鍙橀噺涓轰綘鐨刉ord鏂囨。鐨勫疄闄呰矾寰勶紝灏?code>target_text鍙橀噺鏇挎崲涓轰綘瑕佹彁鍙栫殑鎸囧畾鍐呭銆?/p>

扫码添加客服微信