Skip to content
Snippets Groups Projects
Commit 3b042296 authored by David Verelst's avatar David Verelst
Browse files

remove spaces from empty lines and trailing spaces

parent 627e4f2c
No related branches found
No related tags found
No related merge requests found
......@@ -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__':
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment