在最新版本的openpyxl中获取KeyError:'e'

时间:2019-06-01 13:06:38

标签: python openpyxl

我试图在Windows上通过openpyxl处理电子表格。我相信我的代码是正确的,但是下面的错误无法执行。

我尝试过不同版本的openpyxl,但仍然无法正常工作。 我也尝试重写excel文件。

Traceback (most recent call last):
  File "D:/PycharmProjects/processSpreadsheet/app.py", line 3, in <module>
    process_spreadsheet.process_workbook('transaction.xlsx')
  File "D:\PycharmProjects\processSpreadsheet\process_spreadsheet.py", line 25, in process_workbook
    workbook.save(filename)
  File "C:\Users\jieni\AppData\Local\Programs\Python\Python37-32\lib\site-packages\openpyxl\workbook\workbook.py", line 397, in save
    save_workbook(self, filename)
  File "C:\Users\jieni\AppData\Local\Programs\Python\Python37-32\lib\site-packages\openpyxl\writer\excel.py", line 294, in save_workbook
    writer.save()
  File "C:\Users\jieni\AppData\Local\Programs\Python\Python37-32\lib\site-packages\openpyxl\writer\excel.py", line 276, in save
    self.write_data()
  File "C:\Users\jieni\AppData\Local\Programs\Python\Python37-32\lib\site-packages\openpyxl\writer\excel.py", line 76, in write_data
    self._write_worksheets()
  File "C:\Users\jieni\AppData\Local\Programs\Python\Python37-32\lib\site-packages\openpyxl\writer\excel.py", line 219, in _write_worksheets
    self._write_drawing(ws._drawing)
  File "C:\Users\jieni\AppData\Local\Programs\Python\Python37-32\lib\site-packages\openpyxl\writer\excel.py", line 142, in _write_drawing
    self._archive.writestr(drawing.path[1:], tostring(drawing._write()))
  File "C:\Users\jieni\AppData\Local\Programs\Python\Python37-32\lib\site-packages\openpyxl\drawing\spreadsheet_drawing.py", line 283, in _write
    anchor = _check_anchor(obj)
  File "C:\Users\jieni\AppData\Local\Programs\Python\Python37-32\lib\site-packages\openpyxl\drawing\spreadsheet_drawing.py", line 224, in _check_anchor
    row, col = coordinate_to_tuple(anchor)
  File "C:\Users\jieni\AppData\Local\Programs\Python\Python37-32\lib\site-packages\openpyxl\utils\cell.py", line 201, in coordinate_to_tuple
    return int(row), _COL_STRING_CACHE[col]
KeyError: 'e'

process_spreadsheet.py

import openpyxl as xl
from openpyxl.chart import BarChart, Reference


def process_workbook(filename):
    workbook = xl.load_workbook(filename)
    sheet = workbook['Sheet1']

    for row in range(2, sheet.max_row + 1):
        cell = sheet.cell(row, 3)
        corrected_price = cell.value * 0.9
        corrected_price_cell = sheet.cell(row, 4)
        corrected_price_cell.value = corrected_price

    corrected_value_for_chart = Reference(sheet,
                                          min_row=2,
                                          max_row=sheet.max_row,
                                          min_col=4,
                                          max_col=4)

    chart = BarChart()
    chart.add_data(corrected_value_for_chart)
    sheet.add_chart(chart, 'e2')

    workbook.save(filename)

app.py

import process_spreadsheet

process_spreadsheet.process_workbook('transaction.xlsx')

我很困惑 KeyError:'e'的含义

0 个答案:

没有答案