Ошибка типа openpyxl в Python - PullRequest
0 голосов
/ 08 апреля 2020

Я хочу определить зарплату и налог работников по профессии. Код таков:

import openpyxl as xl
wb = xl.load_workbook('Book1.xlsx')
sheet = wb['Sheet1']

dictionary = {
    'Boss': 1000,
    'Manager': 670,
    'Secretary': 300,
    'Accountant': 470,
    'Redactor': 590,
    'Chef': 270,
    'Servant': 170
}

for row in range(2, sheet.max_row + 1):
    profession = sheet.cell(row, 2)
    salary = dictionary.get(profession.value, profession.value)
    salary_cell = sheet.cell(row, 3)
    salary_cell.value = salary
    tax = salary * 0.14
    tax_cell = sheet.cell(row, 4)
    tax_cell.value = tax
    net_salary = salary - tax
    net_salary_cell = sheet.cell(row, 5)
    net_salary_cell.value = net_salary

wb.save('Book2.xlsx')

Но я получил эту ошибку:

tax = salary * 0.14 Ошибка типа: неподдерживаемые типы операндов для *: 'NoneType' и ' float '

Затем я попытался преобразовать зарплату в словаре в строку и закодировать так:

wb = xl.load_workbook('Book1.xlsx')
sheet = wb['Sheet1']

dictionary = {
    'Boss': '1000',
    'Manager': '670',
    'Secretary': '300',
    'Accountant': '470',
    'Redactor': '590',
    'Chef': '270',
    'Servant': '170'
}

for row in range(2, sheet.max_row + 1):
    profession = sheet.cell(row, 2)
    salary = dictionary.get(profession.value)
    salary_cell = sheet.cell(row, 3)
    salary_cell.value = salary
    tax = int(salary) * 0.14
    tax_cell = sheet.cell(row, 4)
    tax_cell.value = tax
    net_salary = int(salary) - tax
    net_salary_cell = sheet.cell(row, 5)
    net_salary_cell.value = net_salary

wb.save('Book2.xlsx')

Но теперь:

tax = int (salary) * 0.14 TypeError: аргумент int () должен быть строкой, байтовоподобным объектом или числом, а не NoneType

Как устранить эту ошибку?

1 Ответ

0 голосов
/ 08 апреля 2020

Я решил проблему. Это было о sheet.max_row, когда я печатаю зарплату и профессию, я не получаю ни одной. Затем я пишу 9 (максимальное количество записанных строк в файле) вместо sheet.max_row + 1

...