Background: Excel file with 3 columns and approx 10,000 rows. Columns for: Name, Address and Email. Rows for corresponding data.
Tried the following code to extract every entry in the excel file and save in separate file with the name of the person. Like a "business card".
The .txt
files are generated, but they are empty -- i.e. no data written in the files.
Where is the code going wrong and how can I fix it?
import openpyxl as opx
from openpyxl import load_workbook
fileName = r'\...\Name Extraction Text files\TestBook.xlsx'
sheetName = "Sheet1"
wb2 = load_workbook(fileName)
ws4 = wb2["Sheet1"]
maxRows = sheet.max_row
>>> 10001
maxCol = sheet.max_column
>>> 3
for i in range(1, maxRows + 1):
name = sheet.cell(row=i, column=1).value
outputFile = open(r'\...\ExtractedFiles\{}.txt'.format(name), 'w')
for j in range(1, maxCol + 1):
outputFile.write(sheet.cell(row=i, column=j).value + '\n')
outputFile.seek(0)
#checking for the count
print('Written file number: {}'.format(i))
print('Done writing your business cards.')
EDIT: First tried with the .close()
function, but gives the error I/O operation on closed file.
so this attempt with .seek(0)
.
I'm assuming you want to make a txt file for each row? You need to close the file that has been written before doing another one.
outputFile.close()
You also may want to supply 'w+' to the open function.
Check this out https://www.guru99.com/reading-and-writing-files-in-python.html
ADDITION:
for i in range(1, maxRows + 1):
name = ws4.cell(row=i, column=1).value
outputFile = open('test{}.txt'.format(name), 'w+')
for j in range(1, maxCol + 1):
outputFile.write(ws4.cell(row=i, column=j).value + "\n")
outputFile.close()