2017-11-20 30 views
1

내가 선생님이야 댓글. 나는 내가 배정한 에세이에 대해 논평 한 모든 학생들의 목록과 그들이 말한 것을 원한다. Drive API는 너무 힘들었지 만 zip으로 다운로드하여 XML을 파싱 할 수 있다고 생각했습니다.추출 DOCX는

주석은 주석 텍스트 w:t와 함께, w:comment 태그에 태그가 지정됩니다. 그것은 쉬워야하지만 XML (etree)이 나를 죽이고 있습니다. 튜토리얼을 통해

(공식 파이썬 문서) :

z = zipfile.ZipFile('test.docx') 
x = z.read('word/comments.xml') 
tree = etree.XML(x) 

는 그럼 난 이렇게 :

children = tree.getiterator() 
for c in children: 
    print(c.attrib) 

이 결과 :

{} 
{'{http://schemas.openxmlformats.org/wordprocessingml/2006/main}author': 'Joe Shmoe', '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}id': '1', '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}date': '2017-11-17T16:58:27Z'} 
{'{http://schemas.openxmlformats.org/wordprocessingml/2006/main}rsidR': '00000000', '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}rsidDel': '00000000', '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}rsidP': '00000000', '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}rsidRDefault': '00000000', '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}rsidRPr': '00000000'} 
{} 
{'{http://schemas.openxmlformats.org/wordprocessingml/2006/main}val': '0'} 
{'{http://schemas.openxmlformats.org/wordprocessingml/2006/main}val': '0'} 
{'{http://schemas.openxmlformats.org/wordprocessingml/2006/main}val': '0'} 

그리고이 후

나는 완전히 오전 붙어있어. 나는 element.get()element.findall()을 사용해 보았습니다. 값 ( '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}val')을 복사/붙여 넣기해도이 반환됩니다.

아무도 도와 줄 수 있습니까?

+0

요소의 텍스트 내용은 '텍스트'속성이다. 'print (c.text)'는 흥미로운 것을 생성합니까? – mzjn

+0

'A = tree.get ("{} http://schemas.openxmlformats.org/wordprocessingml/2006/main 브로') 인쇄 (a.text)' 결과에 대해 'C'를 AttributeError' 어린이 : 인쇄 (c.text) 코멘트에' 결과! 내가 다른 분야에 어떻게 접근 할 수 있는지 알고 있니? –

답변

1

당신은 현저까지 OOXML은 복잡한 형식임을 고려되었다.

여기 XPath를 통해 DOCX 파일의 코멘트에 액세스하는 방법을 보여주는 몇 가지 예제 파이썬 코드입니다 :

from lxml import etree 
import zipfile 

ooXMLns = {'w':'http://schemas.openxmlformats.org/wordprocessingml/2006/main'} 

def get_comments(docxFileName): 
    docxZip = zipfile.ZipFile(docxFileName) 
    commentsXML = docxZip.read('word/comments.xml') 
    et = etree.XML(commentsXML) 
    comments = et.xpath('//w:comment',namespaces=ooXMLns) 
    for c in comments: 
    # attributes: 
    print(c.xpath('@w:author',namespaces=ooXMLns)) 
    print(c.xpath('@w:date',namespaces=ooXMLns)) 
    # string value of the comment: 
    print(c.xpath('string(.)',namespaces=ooXMLns)) 
+0

답변과 칭찬! 고맙습니다! –