使用 openpyxl 顺序将数据写入 Excel-Sheet 无效

Writing data into Excel-Sheet sequentially using openpyxl isn't working

我正在尝试从 sheet 读取数据,将该数据用作函数中的变量,最后将输出写入 sheet。我已经设法让它的前半部分工作但无法将输出写入 sheet:

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')

代码无限期运行,没有任何错误,但没有对 sheet 做任何事情,所以很难找出我哪里出错了。

我尝试在 python if column_cell[0].value == column_name2: 之前添加 print(h.mediacount) 并且它完美地循环了它所以我相信它一定是下面的代码和写入工作簿的问题。

IIUC,您正在尝试获取列中每个用户名的主题标签,然后获取关联的媒体计数并将此值写入列 'hashtags' 的行中。我认为你让事情变得比他们需要的更复杂。您的代码存在的问题如下:

column_name = 'username'
column_name2 = 'hashtags'

for column_cell in sheet.iter_cols(1, sheet.max_column):
    if column_cell[0].value == column_name:
    # we are now inside col with 'username' as header
    
        B = 0
        for data in column_cell[1:]:
            htag = data.value
            h = Hashtag.from_name(l.context, htag)
            
    # now you are trying to access col 'hashtags', but the below if statement
    # will never be TRUE, since we are INSIDE col 'username'
    
            if column_cell[0].value == column_name2:
            
    # i.e. the following block is unreachable. But even if we WERE to reach it, 
    # it won't do what you want. This would populate the ENTIRE range with 
    # 'h.mediacount' for username1, then overwrite it for username2, etc.
    # at the end you would simple have n rows with h.mediacount for your last
    # username. This is clearly not what you want.
    
                C = 0
                for cell in column_cell[1:]:
                    cell.value = h.mediacount
                    
    # finally, you need to unindent 'book.save()'. Again, we are never
    # reaching this line, but if we WERE to reach it, you would be saving
    # your wb countless times, since it is part of the nested for loop
    
                    book.save('alpha list test.xlsx')

建议的解决方案。找到 'username' 为 header 的列。只循环遍历这个 col(sheet[col][1:]),得到 hashtag 和 mediacount,然后将值写入 col with 'hashtags' 的相应行。我认为这应该有效:

import openpyxl

# import helper function to get column letter
from openpyxl.utils import get_column_letter

from instaloader import Hashtag, Instaloader

filename = "wb.xlsx"

book = openpyxl.load_workbook(filename)
sheet = book['Sheet1']

l = Instaloader()

column_name = 'username'
column_name2 = 'hashtags'

# get a list of all your headers
headers = [cell.value for cell in sheet[1]]

# find 'username' in headers and get col letter
col = get_column_letter(headers.index(column_name)+1)

# get col index for 'hashtags'
col2 = headers.index(column_name2)+1

# loop through usernames
for cell in sheet[col][1:]:
    htag = cell.value
    h = Hashtag.from_name(l.context, htag)
    
    # populate rows in col 'hashtags'
    sheet.cell(cell.row,col2).value = h.mediacount

# AFTER the loop, save your wb
book.save('alpha list test.xlsx')