2017-10-24 1 views
1

"Test_Plan"という名前のフォルダが1つあります。複数のdocxファイルで構成され、各docxファイルには複数のテーブルがあります。私の質問は、どのように私はdocxファイル全体を読むことができると出力を与えるのですか?たとえば、すべてのdocxファイルは複数のテーブルを持って、私は1つのdocxファイルを選ぶと同じフォルダ内の複数のdocxファイルのテーブルをPythonで読み取る方法

(IE)のような出力が得られていますテーブルの
総数:YES自動化の52
総数:6
総数NOオートメーションの:5

このように私はその "Test_Plan"フォルダ内のファイルの数を自動化する必要があります。 あなたは私の質問を理解することを願っています。

単一のdocxファイルからの読み込みテーブルの私のコード:

#Module to retrive the word documents 

from docx import Document 
doc = Document("sample2.docx") 


#Reading the tables in the particular docx 

i = 0 
for t in doc.tables: 
    for ro in t.rows: 
     if ro.cells[0].text=="ID" : 
      i=i+1 
print("Total Number of Tables: ", i) 


#Counting the values of Automation 
# This will count how many yes automation 

j=0 
for table in doc.tables: 
    for ro in table.rows: 
     if ro.cells[0].text=="Automated Test Case" and (ro.cells[2].text=="yes" or ro.cells[2].text=="Yes"): 
      j=j+1 
print("Total Number of YES Automations: ", j) 


#This part is used to count the No automation values 

k = 0 
for t in doc.tables: 
    for ro in t.rows: 
     if ro.cells[0].text=="Automated Test Case" and (ro.cells[2].text=="no" or ro.cells[2].text=="No"): 
      k=k+1 
print("Total Number of NO Automations: ", k) 

出力:

enter image description here

答えて

1

あなたはすべてのファイルを検索するためにグロブを使用することができ、例えば:

import glob 
for name in glob.glob('Test_Plan/*.docx'): 
    doc = Document(name) 
    ... 

globは指定されたパターンに一致するファイル名のリストを返します。上記のように、forループでリストをループし、すべてのファイルを順番に開くことができます。ファイルを開いたら、コードをプラグインするだけです。 もちろん、ループの前に変数を初期化する必要があります。私は、次のアプローチを使用することをお勧めファイル名を分割する

import os.path 

path, filename = os.path.split(input) 
+0

うん、少し後で取り戻します。 –

+0

こんにちはAndreas私は非常にPythonに新しいです。少し高く説明してください。 –

+0

私の解説を私の答えに加えてください。 –

関連する問題