pythonexcelparsingopenxls

openpyxl 'Worksheet' object has no attribute 'write'(python)


Sorry for my English. I need to open a xlsx document and write in the last position new values. But I don't understand how to do it. My algorithm works like this:

  1. Open xlsx l_workbook = load_workbook(old_log_tmp_path)
  2. Get all value from there

    Code:

    def iter_rows(ws):
        for row in ws.iter_rows():
            yield [cell for cell in row]
    
  3. Create new xlsm file

    Code:

    workbook = xlsxwriter.Workbook(tf.name)
    worksheet = workbook.add_worksheet()
    
  4. Copy all values from l_workbook to workbook -> worksheet


But I think it is not right, I think they exist in a simple way. Like this:

 l_workbook = load_workbook('EX2cRqM7xi1D.xlsx')
 sheet = l_workbook.get_sheet_names()[0]
 worksheet = l_workbook.get_sheet_by_name(sheet)
 worksheet.write(1, 1, "TEST")

Running that script gave me the error below:

AttributeError: 'Worksheet' object has no attribute 'write'

My question is: How can I open xlsm file and add new values to it (using openpyxl)?

UPD:

i try this code, but not work

import openpyxl

    workbook = openpyxl.load_workbook('tmp3by148hj.xlsx')
    ws = workbook.worksheets[0]

    ws.cell(row=1, column=1).value = 'TEST'

Solution

  • You need to write into a cell:

    worksheet.cell(row=1, column=1).value = 'TEST'
    

    and finally save your changes:

    workbook.save('tmp3by148hj.xlsx')