Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os, re, xlsxwriter
- def main(folderPath, outputFile):
- '''
- :param folderPath: Where text files are
- :param outputFile: Where excel file goes
- :return: None
- '''
- # get all files
- files = [os.path.join(folderPath, i) for i in os.listdir(folderPath) if i.endswith(".txt")]
- data_dict = find_names(files)
- createTable(data_dict, outputFile)
- def find_names(files):
- return_dict = {}
- for textFile in files:
- with open(textFile) as txt:
- content = txt.read()
- result = re.findall(r'a\) neve: (.*)', content)
- if result:
- return_dict[textFile] = []
- for text in result:
- # text_list = [i.strip() for i in text.split(",")]
- return_dict[textFile].append(text)
- return return_dict
- def createTable(data_dict, outFile):
- workbook = xlsxwriter.Workbook(outFile)
- worksheet = workbook.add_worksheet()
- # Widen the first column to make the text clearer.
- worksheet.set_column('A:A', 50)
- counter = 0
- for fileName, names in data_dict.items():
- worksheet.write(counter, 0, fileName)
- for index, name in enumerate(names):
- worksheet.write(counter, index+1, name)
- counter += 1
- workbook.close()
- main(r"E:\_PythonSuli\0413\Master1\text_to_excel", r"E:\_PythonSuli\0413\Master1\text_to_excel\names.xlsx")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement