diff --git a/wetb/prepost/write_master.py b/wetb/prepost/write_master.py index badf3bcd534ed8fa78726dff90fbf6af39bae31e..57e617ae94f7afc8edd81ca11c46e10a34b0e578 100644 --- a/wetb/prepost/write_master.py +++ b/wetb/prepost/write_master.py @@ -33,7 +33,7 @@ def write_master(path_to_texts, excel_name='DLCs.xlsx', file_end='.txt', delimiter='\t'): """ Write a master Excel sheet from a series of text files - + Args: path_to_texts (str): path to directory with text files excel_name (str): filename of generated master Excel file @@ -44,33 +44,33 @@ def write_master(path_to_texts, # formatting for header cells header_dict = {'bold': True, 'font_color': '#1F497D', 'bottom': 2, 'bottom_color': '#95B3D7'} - + # get list of text files text_files = [f for f in os.listdir(path_to_texts) \ if f.endswith(file_end)] - + # check if main text file in the specified directory if 'Main'+file_end not in text_files: raise ValueError('\"Main\" file not in CSV directory') - - # rearrange text files so main page is first and everything + + # rearrange text files so main page is first and everything # else is alphabetical text_files.remove('Main'+file_end) text_files = ['Main'+file_end] + sorted(text_files) - + # open excel file writer = pd.ExcelWriter(excel_name, engine='xlsxwriter') - + # create workbook and add formast workbook = writer.book header = workbook.add_format(header_dict) - + # loop through text files for text_name in text_files: - + # define path to csv file text_path = os.path.join(path_to_texts,text_name) - + # read data, write to Excel file, and define worksheet handle text_df = pd.read_table(text_path, delimiter=delimiter, dtype=str, @@ -78,10 +78,10 @@ def write_master(path_to_texts, text_df.to_excel(writer, sheet_name=text_name.rstrip(file_end), index=False, header=False) worksheet = writer.sheets[text_name.rstrip(file_end)] - + # get column widths by calculating max string lenths col_widths = text_df.apply(lambda x: np.max([len(str(s)) for s in x])) - + # add formatting for i_col, width in enumerate(col_widths): worksheet.set_column(i_col, i_col, width) @@ -92,10 +92,10 @@ def write_master(path_to_texts, else: worksheet.set_row(1, cell_format=header) worksheet.set_zoom(zoom=85) - + # save worksheet writer.save() - + return if __name__ == '__main__':