2014-12-02 2 views
0

python-docx 라이브러리를 사용하고 file.docx이라는 문서가 있습니다. 내가 file.xlsxpython-docx로 모든 테이블을 복사하는 방법은 무엇입니까?

내 코드에 .docx 테이블의 모든 내용을 복사해야하는 그러나

from docx import * 
f = open('file.docx') 
d = Document(f) 
ts = d.tables[1] 
print ts 
<docx.table.Table object at 0xb691f7ac> 

입니다 <docx.table.Table object at 0xb691f7ac> 콘텐츠가 포함되어 있지 않습니다. 테이블 내용을 어떻게 얻을 수 있습니까?

답변

0

다음과 같은 시도해야합니다 :

from docx import * 
f = open('file.docx') 
document = Document(f) 
tableList=document.xpath('//w:tbl', namespaces=document.nsmap) 
print tableList 

가 나는 각 페이지가 동일한 테이블을 가지고 여러 페이지 문서를 생성했다 유사한 문제를 발생 Extracting tables from a DOCX Word document in python

0

에서 영감을하지만, 다른 콘텐츠와 그 안에.

def _combine_docx(documents): 
    ''' 
    Takes a list of docx.Documents and performs a deepcopy on the 
    first table in each document and adds the copies successively to 
    the first document in the list. 
    ''' 
if not documents or len(documents) == 0: 
    return docx.Document() 
if len(documents) == 1: 
    return documents[0] 

merged_document = documents[0] 
p = merged_document.paragraphs[0] 
for doc in documents[1:]: 
    doc_table = doc.tables[0] 

    new_tbl = deepcopy(doc_table._tbl) 
    p._p.addnext(new_tbl) 

return merged_document 
:

나는 다음과 같이 진행
관련 문제