Попытка обновить цены с противоречивыми данными в Excel, используя openpyxl - PullRequest
0 голосов
/ 24 сентября 2019

Я пытаюсь обновить цены в моем листе Excel.Эти цены должны быть найдены в таблицах Excel, отправленных поставщиками, однако их таблица Excel содержит противоречивые данные.Обычно я импортирую цену одного предмета, но у некоторых предметов есть цена только за дело (состоящее из x предметов за дело).У меня есть количество товара, и я пытаюсь создать программу, которая может корректно обновлять мои цены автоматически.

Product Code    Case Price         Unit Price
92526               19                5.5
97056               250               19
97055               145               
97054               200               
925AAT              45.50    
925AAF              40                6.75

import openpyxl
import pprint

# Set up an empty dictionary which will take key, value pairs = product codes and prices respectively
data = {}

# Set up list of product codes with missing prices in Wholesaler A file.
missing_prices = {}

files = {'My_main_file':'my_file.xlsx',
         'File_WholesalerA':'FileA.xlsx'
         }

wb1 = openpyxl.load_workbook(files['My_main_file'])                   
wb2 = openpyxl.load_workbook(files['File_WholesalerA'])                          
sheet1 = wb1.get_sheet_by_name('Master Database')
sheet2 = wb2.get_sheet_by_name('sheetA')

# Collect all product codes in my database spreadsheet and add them as keys to the empty dictionary
for row in range(2, sheet1.max_row + 1):
    code = sheet1['E' + str(row)].value
    price = sheet1['K' + str(row)].value
    data[code] = price

# Get Wholesaler A prices and add them to prices dictionary, overriding the old price. If single price is missing, use
# case price for the time being.
for row in range(2, sheet2.max_row + 1):
    code = sheet2['A' + str(row)].value
    if code in data:
        single_price = sheet2['J' + str(row)].value
        if single_price == 0 or single_price == '':
            missing_prices[code] = 0  # Append code of missing price as key to missing price dict and assign value of 0
            case_price = sheet2['I' + str(row)].value
            data[code] = case_price
        else:
            data[code] = single_price

# Correct Wholesaler A prices due to using case prices because of missing single prices (I have the number of units per case in my excel file)
for code in missing_prices.keys():
    for row in range(2, sheet1.max_row + 1):
        if sheet1['E' + str(row)].value == code:
            missing_prices[code] = sheet1['I' + str(row)].value

    data[code] = data[code] / missing_prices[code]

# Paste the prices collected into the dictionary into my excel sheet for each #corresponding product code
for row in range(2, sheet1.max_row + 1):
    code = sheet1['E' + str(row)].value
    if code in data:
        sheet1['K' + str(row)].value = data[code]

# Save another version of the spreadsheet with the data
wb1.save('My_main_file v2.xlsx')

pprint.pprint(missing_prices)
pprint.pprint(data)

Когда я печатаю словарь missing_prices, он по какой-то причине возвращается пустым.Я до сих пор не могу понять, почему.

Любая помощь приветствуется.Кроме того, если кто-нибудь может придумать более эффективный способ сделать это, мне было бы интересно посмотреть, как это сделать.Я новичок в программировании и хочу научиться быть более эффективным с моим кодом.

1 Ответ

1 голос
/ 24 сентября 2019

Если в файле Excel пустая ячейка, значение, заданное openpyxl, равно None.Итак, ваш тест должен выглядеть так:

if single_price == 0 or single_price is None:
...