首页 > 解决方案 > 将公式添加到最后一行下方的单元格时出现python错误

问题描述

在一些帮助下,我已经能够编写一个脚本,但是有了这个块,我一直收到一个错误:

import openpyxl
import xlrd
import requests

# loads the workbook - assumes the sussex.xlsx file is in the same directory as the script
wb = openpyxl.load_workbook("sussex.xlsx")

# load first worksheet
ws = wb.worksheets[0]

#go to the British Fencing Association website and download this file (specified)
url = "https://www.britishfencing.com/wp-content/uploads/2018/10/mf_oct_2018.xls"
downloaded_file = requests.get(url)

#write the contents to a new file called rankings.xls
with open("rankings.xls", 'wb') as file:
    file.write(downloaded_file.content)

# Use xlrd to open older style .xls workbook
rank_wb = xlrd.open_workbook('rankings.xls')

# Get the first sheet of the ranked workbook
rank_ws = rank_wb.sheet_by_index(0)

# Get the total number of rows to be used to create our license list
rows = rank_ws.nrows

# Due to formatting, real numbers don't start until here
startrow = 5

# Create list of lic numbers
rank_lic = rank_ws.col_values(colx=4, start_rowx=startrow, end_rowx=rows)

# print the values in the second column of the first sheet
for row in ws['B1:B{}'.format(ws.max_row)]:
    for cell in row:
        print(cell.value)

# Putting values in same row as "Rank, Name, First name,...." adjust as necessary
ws.cell(2, 7).value = 'Fencer NIF'
ws.cell(2, 8).value = 'Points scored'


# Define function to lookup NIF and return value
def get_nif(x):
    startrow = 5
    for y in rank_lic:
        if int(x) == y:
            try:
                return int(rank_ws.cell_value(startrow, 9))
            except ValueError:
                pass
        startrow = startrow + 1

#sum of NIF values

Grand_Total_Row = ws.max_row + 1 
ws.cell(Grand_Total_Row, 1).value = "Grand Total"
ws.cell(Grand_Total_Row, 4).value = "=SUM(G4:G{})".format(ws.max_row - 1)

for row in ws['D3:D{}'.format(ws.max_row)]:
    for cell in row:
        nif_val = get_nif(cell.value)
        ws.cell(cell.row, 7).value = nif_val

# While testing I always save to a different workbook so I don't overwrite my test spreadsheet
wb.save('sussex2.xlsx')

错误是:

Traceback (most recent call last):
File "clubranking.py", line 63, in <module>
nif_val = get_nif(cell.value)
File "clubranking.py", line 48, in get_nif
if int(x) == y:
ValueError: invalid literal for int() with base 10: '=SUM(G4:G35)'

我想要做的是将单元格 G4 带到该列中具有值的最后一个单元格,并在其下方的行上对这些值求和。

有没有人对如何解决这个问题有任何想法?

仅供参考,我正在使用请求;xlrd; 和openpyxl

标签: pythonexcelopenpyxlxlrd

解决方案


您的问题是您使用的库与 Excel文件一起使用,这与在 Excel程序中使用并不完全相同。Excel 程序会自动完成很多 openpyxl 或 xlrd 无法完成的事情。

例如,关于 Excel 文件,您必须了解的一件事是公式及其结果是两个完全不同的东西。它们分别存储并独立读取。在 openpyxl 中,如果您将公式写入单元格,则该单元格中存储了一个公式。它不会(也不能)计算公式的结果。

类似地,当读取单元格时,openpyxl 会您提供公式结果,但不能同时提供两者(您必须选择您想要的;默认情况下,如果有公式,您会得到公式)。xlrd 只会给你结果。而且我怎么强调都不过分:在你的情况下,没有结果可以读取,因为它根本没有被计算出来。因此,即使您告诉 openpyxl 给您结果而不是公式,那也无济于事。

相反,Excel程序(默认情况下)总是重新计算公式,并同时存储公式及其结果,使它们保持同步。您正在使用的库不是这种情况。

您显示的错误消息是您尝试转换'=SUM(G4:G35)'为整数时得到的。请注意,Python 的int函数不知道 Excel 是什么、单元格是什么或公式是什么。它正在尝试转换等号,大写“S”,大写“U”,大写“M”,左括号,大写“G”,数字4,冒号,另一个大写“G”,数字 3 和 5,以及将右括号转换为整数。Python 告诉您,它无法将其理解为整数。

如果您想继续使用 Python 执行此操作,您的主要选择是(1)在 Python 中自己计算总和并使用它;或 (2) 使用不同的库,例如xlwings,它将与 Excel程序一起使用,而不是原始文件。


推荐阅读