![]() |
Python3 doesn't populate xlsx file with openpyxl - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Python3 doesn't populate xlsx file with openpyxl (/thread-32511.html) |
Python3 doesn't populate xlsx file with openpyxl - Auldyin75 - Feb-14-2021 Hi, Recently I acquired a raspberry pi 3 which my grandson got bored with and decided to make a simple weather station. The goals are: 1. To connect a temperature/pressure/humidity/sensor to the raspberry 2. Collect readings 3. The readings should be visible on screen 4. A pause (sleep) value can be changed to suit 5. The readings should also be saved to a spreadsheet. I am completely new to Python (did some html in my time purely for my own website) So far, however , although I have succeeded with goals 1,2,3 , 4, but can't get readings to appear in my spreadsheet. Raspberry pi 3 Pi OS = Raspbian GNU UNIX 10 Buster Python 3.7.3 Thonny 3.3.3 The .py file and the xlsx file are in the same folder [i]The script below runs without error , but data does not appear in the spreadsheet. If I populate the 1st row of spreadsheet with some random numbers , it gets wiped and replaced with blanks. I have spent 2 or 3 weeks with different versions to achieve the different goals .... this version achieves all goals except one import smbus2 import bme280 from time import sleep import time import datetime from datetime import date #installed openpyxl at terminal with "sudo pip3 install openpyxl" from openpyxl import * amounts, row = [1, 2, 3, 4, 5], 2 workbook = load_workbook("/home/pi/weather-station/bme280_data.xlsx") port=1 address=0x76 bus=smbus2.SMBus(port) def update_temp(): temperature = p.read_temp_c() temp_text.value = temperature temp_text.after(1000, update_temp) #load the workbook and select the sheet workbook = workbook filename = "/home/pi/weather-station/bme280_data.xlsx" sheet = workbook.active today=date.today now=datetime.datetime.now().time() bme280_data=bme280.sample(bus,address) humidity=bme280_data.humidity pressure=bme280_data.pressure ambient_temperature=bme280_data.temperature # for i, value in enumerate(amounts): sheet.cell(column=i+1, row=row, value=value) # counter=1 while 1: if not counter % 10: bme280_data=bme280.sample(bus,address) humidity=bme280_data.humidity pressure=bme280_data.pressure ambient_temperature=bme280_data.temperature now=datetime.datetime.now().time() print('Adding sensor data to spreadsheet:') print(today) print(now) print('Humidity:'+str(round(humidity,2))) print('Pressure:'+str(round(pressure,2))) print('Ambiant_Temperature:'+str(round(ambient_temperature,2))) time.sleep(10) #append data to spreadsheet row=(today, now, ambient_temperature, pressure, humidity) sheet.append(row) #Save the workbook workbook.save('/home/pi/weather-station/bme280_data.xlsx') counter += 1 #increment counter by 1 RE: Python3 doesn't populate xlsx file with openpyxl - buran - Feb-14-2021 On line 36-50 you have infinite loop. Inside the loop you never update workbook and you never break out of the loop. By the way I don't see the purpose of update_temp function and where it is used.
RE: Python3 doesn't populate xlsx file with openpyxl - Auldyin75 - Feb-16-2021 Thanks Buran |