Python Forum
Python3 doesn't populate xlsx file with openpyxl
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python3 doesn't populate xlsx file with openpyxl
#1
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
buran write Feb-14-2021, 06:26 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Thanks Buran
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Last record in file doesn't write to newline gonksoup 3 402 Jan-22-2024, 12:56 PM
Last Post: deanhystad
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,407 Nov-09-2023, 10:56 AM
Last Post: mg24
  how to populate a dataframe thru line iteration ? knob 0 992 May-05-2022, 12:48 AM
Last Post: knob
  Populate Dropdown list from Database TommyAutomagically 4 4,489 Nov-02-2021, 05:23 PM
Last Post: ndc85430
  Convert legacy print file to XLSX file davidm 1 1,796 Oct-17-2021, 05:08 AM
Last Post: davidm
  guys please help me , pycharm is not able to identify my xlsx file CrazyGreenYT7 1 2,005 Jun-13-2021, 02:22 PM
Last Post: Larz60+
  Auto-populate Macro variables Spartan314 3 2,630 Mar-08-2021, 12:36 AM
Last Post: Spartan314
  create new tabs and populate using python reggie4 2 2,249 Jan-23-2021, 11:25 PM
Last Post: Larz60+
  Populate a table with variables Market_Python 4 2,560 Jan-11-2021, 09:45 AM
Last Post: Pedroski55
  P3, openpyxl, csv to xlsx, cell is not number, problem with colorize genderbee 1 2,123 Sep-29-2020, 03:20 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020