0

I want to input date of text and sum function in excel file. The excel file content is changed everyday, so I don't know which row is right position in excel file.

The image of the excel file is the output that I want to do. The image is as followed. enter image description here

In this excel file, I need to input text in A29, but the excel amount is changed next time. This is the coding what I did. If you have any idea, please let me know that.

This is the coding that I did as followed.

ws['ws.max_row + 9',1] = '합계'

OR

z = ws.cell(row=ws.max_row + 9, column=1).value

Both have error occurred.

문정아
  • 141
  • 11
  • If I understand correctly, you want to enter a value on the 9th row after the last row in an excel sheet using openpyxl? – shoaib30 Jul 23 '21 at 07:30

1 Answers1

1

If you want to write to a row 9 rows after the last row in the excel sheet. You can get the last row using ws.max_row as you have done and add 9 to it.

You are on the right track. When accessing a cell using ws[] you need to pass a cell identifier as used in Excel like - ws['A1'] If you need to access a cell using row and column numbers, you may do so with the ws.cell() function as you have in the second example. you only need to interchange RHS and LHS in your code snippet.

The below code should achieve what you need

from openpyxl import load_workbook

wb = load_workbook(filename='sample.xlsx')
ws = wb.active
last_row = ws.max_row
ws.cell(row=last_row + 9, column=1).value = 'New Value'
wb.save(filename='sample.xlsx')

Source: https://stackoverflow.com/a/33543305/5236575

shoaib30
  • 877
  • 11
  • 24