Python-Excel:查找列中的第一个空行 [英] Python - Excel: Finding the first empty row in a column

查看:755
本文介绍了Python-Excel:查找列中的第一个空行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从我的最后一个问题开始,我设法获得了很多完成系统的方法.当然,我遇到了一个问题.

working from my last question I've managed to get a good chunk of the way to get my system finished. Of course I've run across a problem.

我基本上有一个玩游戏的程序.每个正确答案都会为全局变量点"加10.然后,我想在Excel电子表格中添加点".

I basically have a program that plays a game. Every correct answer adds 10 to the global variable 'points'. Then I want to add 'points' into an excel spreadsheet.

这是我很困的地方.我正在运行XLRD-0.8.0,XLUTILS-1.4.1和XLWT-0.7.5.

This is where I get very stuck. I'm running XLRD-0.8.0, XLUTILS-1.4.1 and XLWT-0.7.5.

我当然查找了不同的东西,但是它们似乎对我不起作用.

Of course I've looked up different things but they don't seem to work for me.

这是我的代码的简化版本:

This is a simplified version of my code:

 import pygame, pygame.font, pygame.event, string, xlwt, xlrd, xlutils, socket


 points = 0

 def Uploadpoints(wbname):

     global points 
     wb = xlrd.open_workbook(wbname)

     # CODE TO FIND FIRST EMPTY CELL IN COLUMN 1 GOES HERE

     wb.write(row,0,points)
     wb.save()

 Uploadpoints('workbook1.xls')

我想做这样的事情,但是我不确定该如何处理,所以我发布了伪代码.

I thought of doing something like this but I'm not sure how I would go about it so I post pseudo-code.

定义工作表:ws = 'sheet 1' [完成]

Define worksheet: ws = 'sheet 1' [Done]

定义列:col = 0 [column A] [完成]

搜索col中为空的第一行:??? [未完成]

Search for first row in col that is empty: ??? [Not Done]

将第一行定义为空:row = ????? [未完成]

Define first row that is empty as row: row =????? [Not Done]

任何帮助将不胜感激.预先感谢.

Any help would be greatly appreciated. Thanks in advance.

推荐答案

我对基本上,出现问题的地方是xlrdxlwt是具有不同对象的不同模块.您使用xlrd.open_workbook() 不是读取的对象与xlwt知道如何写入

Basically whats going wrong is that xlrd and xlwt are different modules with different objects. The object you read in with xlrd.open_workbook() IS NOT the same object which xlwt knows how to write to

要解决此问题,请在xlutils中使用复制功能.

To get around this, theres the copy function in xlutils.

from xlutils.copy import copy
import os

wb = xlrd.open_workbook(name) 

# Code to find the last open cell

wb = copy(wb)   #This is the important line!

sheet = wb.get_sheet(num)  #num is the index of the sheet you want to edit
sheet.write(whatever you want to write)

wb.save(name)

这篇关于Python-Excel:查找列中的第一个空行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆