Nov-08-2023, 10:22 AM
Hello,
Ok, i can't get the code to work, to ensure that i have not altered anything i have copied the code again here:-
Also, it maybe a good idea for me to add the new lines at the top of the XL sheet so i will keep that idea, thanks.
Ok, i can't get the code to work, to ensure that i have not altered anything i have copied the code again here:-
import openpyxl from datetime import datetime # this XL has 1 sheet called test path2XL = '/home/john/Desktop/test.xlsx' wb = openpyxl.load_workbook(path2XL) #maxRow = wb['test'].max_row maxCol = wb['test'].max_column # show the headers for i in range(1, maxCol + 1): print('The column headers are: ', wb['test'].cell(row=1, column=i).value) # row 1 is presumably the headers so insert a row at 2 to put the latest data in row 2 """ from here put all this in a loop """ wb['test'].insert_rows(2) # get time and date dt = datetime.now() cdate = dt.strftime('%x') ctime = dt.strftime('%X') # put values in row 2 # in this XL column 1 is id an integer starting at 1 # this increments id upwards, row 2 is the highest id, the latest data (if that is the goal) idd = wb['test'].cell(row=3, column=1).value + 1 # not sure how you are getting rowlist but len(rowlist) should probably be = maxCol rowlist = [idd, 'V', 'indifferent', ctime, cdate] for i in range(1, maxCol + 1): wb['test'].cell(row=2, column=i, value=rowlist[i-1]) wb.save(path2XL) print(f'Added a row to {path2XL} at row 2.')I ran the code and i get the following results
Output:>>> %Run -c $EDITOR_CONTENT
The column headers are: Head 1
The column headers are: Head 2
The column headers are: Head 3
The column headers are: Head 4
The column headers are: Head 5
Traceback (most recent call last):
File "<string>", line 23, in <module>
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
>>>
Line 23 is as indicated above and i am struggling to fix the issue ????Also, it maybe a good idea for me to add the new lines at the top of the XL sheet so i will keep that idea, thanks.