python 向最后一行下方的单元格添加公式时出错

python error when adding a formula to a cell below the last row

在一些帮助下,我已经能够将脚本放在一起,但是对于这个块,我不断收到错误消息:

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

你的问题是你正在使用的库与 Excel files 一起工作,这与在 Excel 中工作不完全一样程序。 Excel 程序会自动完成很多 openpyxl 或 xlrd 无法完成的事情。

例如,关于 Excel 文件,您必须了解的一件事是公式及其结果是两个完全独立的事物。它们分开存储并独立读取。在openpyxl中,如果你把公式写到一个单元格,那么那个单元格只有有一个公式存储在里面。它不会(也不能)计算公式的结果。

类似地,当需要读取单元格时,openpyxl 将 或者 给你公式 结果,但不会同时给你(你必须选择你想要的;默认情况下,如果有的话,你会得到公式)。 xlrd 只会给你结果。我再怎么强调也不为过:在你的情况下 没有结果可以读取 因为它根本没有被计算出来。所以即使你告诉 openpyxl 给你结果而不是公式,那也帮不了你。

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

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

如果你想继续用 Python 这样做,你的主要选择是 (1) 在 Python 中自己计算总和,然后使用它;或 (2) 使用不同的库,例如 xlwings,它将与 Excel 程序 一起工作,而不是原始文件。