I am looking at a large dataset of thousands of .xls files and need to open each one and look at the contents of certain cells based on what is found in a different cell within the same row. There are thousands of .xls files to analyze so I've created (or attempted to create, rather) a Python script to do it. The .xls format is a requirement. The code I've produced does a great job of printing to console the answers but I cannot get a useful spreadsheet output. The current code will only write the output of the final file inspected and does so on the row number correlating to the iteration count of the last file in the dataset.
For instance, let's assume we have 3 files. For the 3 files let's assume the following data structure:
File1 File2 File3
UniqueData1 File1Data1 File2Data1 File3Data1
UniqueData2 File1Data2 File2Data2 File3Data2
UniqueData3 File1Data3 File2Data3 File3Data3
In this case, the output file looks like this, with 2 blank lines and data only in the 4th (including header row) and final row representing only the third file:
col1 col2 col3
row1 UniqueData1 UniqueData2 UniqueData3
row2
row3
row4 File3Data1 File3Data2 File3Data3
How can I change this code such that the output file records the findings of each .xls file in a new row?
from pathlib import Path
import os
import xlrd
import xlwt
Data_Path = Path("/file_path")
def func1():
global UniqueData1
for row_num in range(worksheet.nrows):
row_value = worksheet.row_values(row_num)
if row_value[0] == "1400":
UniqueData1 = row_value[5]
Results.append(UniqueData1)
def func2():
global UniqueData2
for row_num in range(worksheet.nrows):
row_value = worksheet.row_values(row_num)
if row_value[0] == "5229":
UniqueData2 = row_value[5]
Results.append(row_value[5])
def func3():
global UniqueData3
for row_num_sn in range(worksheet.nrows):
row_value = worksheet.row_values(row_num_sn)
if row_value[0] == "1421":
UniqueData3 = row_value[5]
Results.append(row_value[5])
def WritetoFile(spreadsheetrow):
Results_Path = Path("/file_path/results")
os.chdir(Results_Path)
Results_file = xlwt.Workbook()
sheet = Results_file.add_sheet("Sheet1")
sheet.write(0, 0, "UniqueData1")
sheet.write(0, 1, "UniqueData2")
sheet.write(0, 2, "UniqueData3")
sheet.write(spreadsheetrow, 0, UniqueData1)
sheet.write(spreadsheetrow, 1, UniqueData2)
sheet.write(spreadsheetrow, 2, UniqueData3)
Results_file.save("Results.xls")
os.chdir(Data_Path)
File_list = os.listdir(Data_Path)
spreadsheetrow = 1
for filename in File_list:
Results = []
if filename.endswith(".xls"):
read_workbook = xlrd.open_workbook(filename)
worksheet = read_workbook.sheet_by_name("Parameters")
func1()
func2()
func3()
WritetoFile(spreadsheetrow)
print("Results Table Row: " , spreadsheetrow+1)
spreadsheetrow += 1
Also - any and all constructive criticism on the code's layout is more than welcome. I'm a Mechanical Engineer by trade that sees the usefulness of programming but never formally taught so I'm willing to assume this code isn't very "Pythonic".
Thanks for the help!