在迭代列和行时将列表写入现有的 excel 文件

问题描述 投票:0回答:0

我正在尝试将列表写入现有的 Excel 文件。该列表将从第 5 列开始,每四列打印每个列表值(接下来将在第 9 列)。然后,将列表中的每一项写入excel后,列表将被清空并追加新数据。然后程序需要写入与之前完全相同的列,但向下移动了一行。

我尝试了几种方法,但都无法正常工作。我还需要在写入每一行后保存 Excel 文件,并且不确定 ExcelWriter 是否自动执行此操作。这是我目前没有功能的代码。

def start_buy_monitoring():
    excel_file = "MarketBots (1).xlsx"
    row_inc = 0

    excel_output = pd.read_excel(excel_file, sheet_name='Buff163AppliedStickers', usecols=['Buff163 URL', 'Sticker URL Code', 'Sticker Price (Average)'])
    num_rows = len(excel_output.index)

    while(row_inc < num_rows):
        percentages = []

        stickerPrice = excel_output.at[row_inc, 'Sticker Price (Average)']
        adjustedPrices = returnDataBuff163(driver163, excel_output.at[row_inc, 'Buff163 URL'])

        listIncrement = 0

        while(listIncrement < len(adjustedPrices)):
            try:
                print(stickerPrice.text)
                percentages.append(round(adjustedPrices[listIncrement] / float(stickerPrice), 4))

            except:
                percentages.append("Error")
            listIncrement += 1

        if(percentages != False) :
            global percentagesDF
            percentagesDF = pd.DataFrame(percentages)
            startRow = 2
            startCol = 5

            with pd.ExcelWriter('MarketBots (1).xlsx', engine='openpyxl', mode='a', if_sheet_exists='overlay') as writer:
                percentagesDF.to_excel(writer, sheet_name='Buff163AppliedStickers', startrow=(startRow + row_inc), startcol=startCol)

        row_inc += 1

    driver163.quit()

这是我得到的错误:

Traceback (most recent call last):
  File "C:\Users\kumpd\PycharmProjects\pythonProject2\StickerPercentage.py", line 185, in <module>
    start_buy_monitoring()
  File "C:\Users\kumpd\PycharmProjects\pythonProject2\StickerPercentage.py", line 178, in start_buy_monitoring
    percentagesDF.to_excel(writer, sheet_name='Buff163AppliedStickers', startrow=(startRow + row_inc), startcol=startCol)
  File "C:\Users\kumpd\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\util\_decorators.py", line 211, in wrapper
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\kumpd\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\util\_decorators.py", line 211, in wrapper
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\kumpd\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\core\generic.py", line 2374, in to_excel
    formatter.write(
  File "C:\Users\kumpd\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\io\formats\excel.py", line 950, in write
    writer._write_cells(
  File "C:\Users\kumpd\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\io\excel\_openpyxl.py", line 496, in _write_cells
    xcell.value, fmt = self._value_with_fmt(cell.val)
    ^^^^^^^^^^^
AttributeError: 'MergedCell' object attribute 'value' is read-only
python python-3.x pandas openpyxl
© www.soinside.com 2019 - 2024. All rights reserved.