2016-12-09 10 views
0

Python docxモジュールを使用して特定のテーブルデータを解析しようとしています。docxを使用して辞書形式のテーブルデータを解析する

テーブルデータは、私が更なる処理のためにそのデータを使用できるように、キー値形式で「当局」と、それぞれの「バージョン」を取得する必要があり、この enter image description here

ようになります。

私は、私が使用している場合、辞書を反復することができません -

私をorderedDictionary与えますが、私は私が私に 4.5.6

from docx import Document 

document = Document('myfile.docx') 

    for table in document.tables: 
     printTable = False 
     rowNo = 0; 
     for row in table.rows: 
      for cell in row.cells: 
       if cell.text == "Table2": 
        printTable = False 
      if printTable: 
       print (table.cell(rowNo, 0).text + '=' + table.cell(rowNo, 2).text) 
      for cell in row.cells: 
       if cell.text == "Authorities": 
        printTable = True 
      rowNo += 1 
を与えることを期待してい d['Juno'] を使用して値にアクセスするカント
d = OrderedDict(zip(table.cell(rowNo, 0).text, table.cell(rowNo, 2).text)) 

解析後にデータを下位フォーマットで取得しています -

Juno=4.5.6 
Acrux=3.5.6 
Mars=5.6.7 

答えて

1

あなたは辞書を定義し、これを達成することができます -

from docx import Document 

document = Document('myfile.docx') 
data = {} 
for table in document.tables: 
    printTable = False 
    rowNo = 0; 
    for row in table.rows: 
     for cell in row.cells: 
      if cell.text == "Table2": 
       printTable = False 
     if printTable: 
      data[table.cell(rowNo, 0).text] = table.cell(rowNo, 2).text 
     for cell in row.cells: 
      if cell.text == "Authorities": 
       printTable = True 
     rowNo += 1 
print (data) 

はあなたの辞書形式

で期待されるデータを提供します
関連する問題