How to find next empty cell on a specific column and write to it?

2024/10/5 17:17:38

I want to find the next empty cell in a specific column and write to values that cell. I've tried it using following method:

for row in sheet['A{}:A{}'.format(sheet.min_row,sheet.max_row)]:if row is None:sheet.cell(column=1).value = nameelse:print ("Cell have data")

But It's not writing data to next empty cell. How can I fix that?

Answer

It's pretty pointless to construct a string with min_row and max_row. You can simply access the whole column:

from openpyxl import load_workbookwb = load_workbook("book.xlsx")
ws = wb.activefor cell in ws["A"]:if cell.value is None:cell.value = "new value2"wb.save("book.xlsx")

But this reads the whole column at once as a tuple. Instead, you can use iter_rows() (iter_cols() is not available in read-only):

from openpyxl import load_workbookwb = load_workbook("book.xlsx")
ws = wb.activefor row in ws.iter_rows(min_col=1, max_col=1):cell = row[0]if cell.value is None:cell.value = "new value"wb.save("book.xlsx")
https://en.xdnf.cn/q/119188.html

Related Q&A

How can i pass data from template in Django to Javascript in this specific case

I am using Google map api and i am trying to pass the data (longitude and latitude) to the template then use the data in the javascript to show a specific location.location.html{% for venue in propert…

Python Highscores w/ text file

i am currently working on a text based game, and have run into a problem trying to make my last function, highscore. My problem is this, i would like to make the function save my top five scores and sa…

Do any one know about this Error in python? how can I resolve this?

I am plotting the data with MapBoxGl Python Library on maps, here is my code which is taking the latitude, longitude and points from the Pandas DataFrame and trying to make the geojson, here is the cod…

Pandas: Count Higher Ranks For Current Experiment Participants In Later Experiments (Part 1)

Learning Experiments In a series of learning experiments, I would like to count the number of participants in each experiment that improved their performance in subsequent experiments (Rank 1 is highes…

Pass variables into and out of exec [closed for not being clear. Modified and reuploaded] [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 3 years ago.Improve…

List Object Not Callable, Syntax Error for Text-Based RPG

Im having issues (still) with generating a random object, in this case a random herb found by foraging. Heres the code for the function:def collectPlants(self):if self.state == normal:print"%s spe…

Why does my Tkinter window background not change?

Ive a small program with a feature to change the background color of a different window than the frame I use to ask for the background color. (I hope you understand this.) The program is written in Pyt…

Combining a nested list without affecting the key and value direction in python

I have a program that stores data in a list. The current and desired output is in the format:# Current Input [{Devices: [laptops, tablets],ParentCategory: [computers, computers]},{Devices: [touch], Par…

Splitting very large csv files into smaller files

Is Dask proper to read large csv files in parallel and split them into multiple smaller files?

How to make this Python script to run subfolders too?

Which part of the codes do I need to change in order to include subfolders? File handle.py import glob import os import sys from typing import Listdef get_filenames(filepath: str, pattern: str) -> …