xlsx_to_db_count_replace/update_spredsheet.py

37 lines
1.2 KiB
Python
Raw Normal View History

2024-02-10 14:17:43 +02:00
from dev_xlsx import spreadsheet
from models import Price
def fetch_data_from_database():
# Query the database for the data
data = {}
for price in Price.select():
data[price.sku] = price.Количество # Assuming 'sku' is the key
return data
def update_spreadsheet_with_new_values(new_values):
# Open the first worksheet
worksheet = spreadsheet.get_worksheet(0)
# Get all values from the worksheet
values = worksheet.get_all_values()
headers = values[0]
# Find the index of 'Количество' column
quantity_index = headers.index('Количество')
# Update values in the spreadsheet
for row in values[1:]: # Skip the header row
sku = row[0] # Assuming SKU is the first column
if sku in new_values and row[quantity_index] != str(new_values[sku]): # Compare as strings
worksheet.update_cell(values.index(row) + 1, quantity_index + 1, str(new_values[sku])) # Adjust to 1-based index
if __name__ == "__main__":
# Fetch data from the database
data_from_database = fetch_data_from_database()
# Compare values and update the spreadsheet
update_spreadsheet_with_new_values(data_from_database)