2017-10-04 1 views
0

단락 제목을 검색하고 일치시킨 후 파이썬을 사용하여 단어 문서를 검색하려면 어떻게해야합니까? "1.2 Broadspectrum Offer 요약".Python에서 단락 텍스트 추출

아래의 문서를 참조하십시오. "부드러운 문서에 설명 된대로 작업 범위를 제공하겠다는 제안에 대한 요약이 아래에 나와 있습니다. 우리의 제공의 조건은 여기에 설명 된대로. 또한 비용 내역을 찾아주세요 제목 번호 변경 문서에서 문서 및이에 의존하지 않으려는 더 그래서 난 의존하려는 "

1. Executive Summary 

1.1 Summary of Services 
Energy Savings (Carbon Emissions and Intensity Reduction) 
Upgrade Economy Cycle on Level 2,5,6,7 & 8, replace Chilled Water Valves on Level 6 & 8 and install lighting controls on L5 & 6.. 

1.2 Summary of Broadspectrum Offer 

A summary of our Offer to deliver the Scope of Work as outlined in the tender documents is provided below. Please refer to the various terms and conditions of our Offer as detailed herein. 
Please also find the cost breakdown 

노트 제목의 검색 텍스트에

지금까지 문서를 검색 할 수 있지만 시작 만 할 수 있습니다.

filename1 = "North Sydney TE SP30062590-1 HVAC - Project Offer - Rev1.docx" 

from docx import Document 

document = Document(filename1) 
for paragraph in document.paragraphs: 
    if 'Summary' in paragraph.text: 
     print paragraph.text 
+0

'1.2 요약 ...'단락 이후 문서에 아무 것도 없습니까? 그리고'Broadspectrum Offer'의'Summary'는 언제나'1.2'와 함께 쓰여질 것인가? – sadmicrowave

+0

정규식을 작성하려면 re 라이브러리를 사용해야합니다. SO와 웹을 중심으로 광범위한 정보가 있습니다. –

+0

아마도 도움이 될 수 있습니다. https://stackoverflow.com/questions/40388763/extracting-headings-text-from-word-doc –

답변

1

여기에 예비 솔루션이 있습니다 (위 게시물에 대한 내 의견에 대한 답변을 기다리고 있습니다). 이것은 아직Summary of Broadspectrum Offer 섹션 다음에 추가 문단 을 배제하는 것을 설명하지 않습니다. 이것이 필요한 경우, 다른 헤더 섹션에 1.3 (등)이 발생했는지 파악하기 위해 작은 정규 표현식 일치가 필요할 것이고, 그렇다면 이해를 중단하십시오. 이것이 요구 사항인지 알려주세요.

편집 : 아래 Anton vBR의 코멘트에 대한 응답으로, 표준 for 루프에 print() 목록에서 이해의 방법을 변환.

from docx import Document 

document = Document("North Sydney TE SP30062590-1 HVAC - Project Offer - Rev1.docx") 

# Find the index of the `Summary of Broadspectrum Offer` syntax and store it 
ind = [i for i, para in enumerate(document.paragraphs) if 'Summary of Broadspectrum Offer' in para.text] 
# Print the text for any element with an index greater than the index found in the list comprehension above 
if ind: 
    for i, para in enumerate(document.paragraphs): 
     if i > ind[0]: 
      print(para.text)  

[I의 인쇄 (para.text) 열거 파라 (document.paragraphs) IND IF 및 I> IND [0]

>> A summary of our Offer to deliver the Scope of Work as outlined in the tender documents is provided below. 
Please refer to the various terms and conditions of our Offer as detailed herein. 
Please also find the cost breakdown 

또한, 여기에 수있는 다른 게시물 문법 메타 데이터를 사용하여 heading 유형을 감지하는 또 다른 방법을 찾으십시오. Extracting headings' text from word doc

+0

게시 한 행이 길고 목록 내부에서 print()를 사용하는 것은 실제로 권장되지 않습니다. . –

+0

'print()'함수로리스트 이해력을 제거했습니다. 그것은 단순히 원했던 것을 인쇄하기위한 청결한 라이너 였지만 당신이 옳았습니다. 그것은 최선의 방법이 아닙니다. – sadmicrowave