问题
I am trying to update prices in an excel sheet of mine. These prices should be found in excel spreadsheets sent by suppliers, however their excel sheet has inconsistent data. I generally import the price of a single item but some items only have a price for a case (consisting of x items per case). I have the item quantities and am trying to create a program that can correctly update my prices automatically.
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)
When I print the missing_prices dictionary it comes back blank for some reason. I still couldn't figure out why.
Any help is appreciated. Also, if anyone can think of a more efficient way of doing this I would be curious to see how. I am new to programming and want to lear how to be more efficient with my code.
回答1:
If a cell is empty in the Excel file, the value given by openpyxl
is None
. So your test should look like:
if single_price == 0 or single_price is None:
来源:https://stackoverflow.com/questions/58083260/trying-to-update-prices-with-inconsistent-data-in-excel-using-openpyxl