'Outputted data into a xlxs sheet's row in sequence with openpyxl

I'm trying to write the data outputted h.mediacount to a column (C) in Sheet1.

I can't figure out how to iterate through to the next cell for the next output i.e writing h.mediacount to cell C2, looping and writing the next output to cell C3 etc.

Here is my code as it stands.

book = load_workbook(path)
sheet = book['Sheet1']
column_name = 'username'
for column_cell in sheet.iter_cols(1, sheet.max_column):
    if column_cell[0].value == column_name:
        B = 0
        for data in column_cell[1:]:
            htag = data.value

            print(htag)
            h = Hashtag.from_name(l.context, htag)
            print(h.mediacount)

Please note the print(htag) and print(h.mediacount) are only there to demonstrate that the code works up to that point.

Update: I've written this code out, however, it runs indefinitely without any errors, but also without any changes to the sheet. I am unable to see where it's going wrong as there are no errors.

column_name = 'username'
column_name2 = 'hashtags'

for column_cell in sheet.iter_cols(1, sheet.max_column):
    if column_cell[0].value == column_name:
        B = 0
        for data in column_cell[1:]:
            htag = data.value
            h = Hashtag.from_name(l.context, htag)
            if column_cell[0].value == column_name2:
                C = 0
                for cell in column_cell[1:]:
                    cell.value = h.mediacount
                    book.save('alpha list test.xlsx')

Update 2: Tried adding print(h.mediacount) before python if column_cell[0].value == column_name2: and it loops through that flawlessly, must be an issue with the code underneath and writing to the workbook.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source