I'm trying to perform two actions:
1) Check to see if a worksheet exists in a workbook using xlwings
2) Use a variable name to activate the worksheet using xlwings.
The worksheet name is a variable, so I can't use the sheets[0] option or sheets['name'] option.
import xlwings as xw
app = xw.apps.active
wb = app.books.active
key1 = 'BUS'
if key1 in wb:
sht = wb.sheets.activate(key1)
else:
sht = wb.sheets.add(key1)
I get the error:
AttributeError: 'Sheets' object has no attribute 'activate'
You should slightly rewrite your code to get this working. Tested this by opening a new Excel workbook and running the code a few times.
# python 3.7.3
# xlwings 0.15.8
import xlwings as xw
app = xw.apps.active
wb = app.books.active
key1 = 'BUS'
if key1 in [sh.name for sh in wb.sheets]:
sht = wb.sheets[key1]
else:
sht = wb.sheets.add(key1)
Changes:
You should iterate over the sheet names instead of the sheet objects
There is no need to activate a sheet in xlwings. As soon as you assign the sheet you want to work in to your sht variable, all actions will be performed on this sheet. You can also create a separate variable for every sheet (e.g. sht_bus, sht_train, ...)
Related
Is it possible in python to rename an active workbook and sheet?
I currently am using the xlwings "view" command to open my data frame up in excel which is nice but would like to have the active workbook/sheet named without saving it to a specified directory.
import xlwings as xw
xw.view(df)
When I try overriding the fullname of the active workbook I get an attribution error
xw.books.active.fullname = "Report"
AttributeError: can't set attribute
To rename the active Sheet in active Workbook you can try:
xw.books.active.sheets.active.name = "test"
I'm using python 2.7 with Tkinter (new to Tkinter:))
I have UI with list of 20 checkboxes
once I click on one checkbox, all checkboxes are being checked, instead of one.
In code below you'll see 2 line (once with #)
with # -when click on checkbox only one is checked which is ok
without # -when click on one, all are being checked
The problem is that I want to know the status of each checkbox if checed or not and I have to define var=Intvar in order to "get" it status
Any suggestion?
Thanks in advance
Below is relevant def
def suites_checkbox_create(self):
ExcelWorkBook1 = open_workbook(config.UI_Suites_Location + 'STD_SUITES.xlsx', on_demand=True)
First_Sheet1 = ExcelWorkBook1.sheet_by_index(0)
plushight = 110
suitesList=[]
self.CheckboxList=[]
for name in (First_Sheet1._cell_values):
if name[3] == "General Name":
continue
else:
suitesList.append(name[3])
for index, name in enumerate(suitesList):
self.var=IntVar
#self.CheckboxList.append(Checkbutton(self.app, text=name)) # using this, i can check once checkbox a time
self.CheckboxList.append(Checkbutton(self.app, text=name, variable=self.var)) # with this, once i check once checkbox, all checkboxes(20) are bing checked
self.CheckboxList[index].place(y=plushight)
plushight += 20
The reason this happens is because you've given all of your Checkbutton widgets the same variable for their variable attribute.
Meaning that as soon as one of the Checkbutton widgets is ticked self.var is given a value of 1 which means that all of the Checkbutton widgets have a value of 1 which equates to them having been selected.
In short, whenever one is ticked it updates the value of all the other's because they have the same variable used to store their value.
See this in the example below:
from tkinter import *
root = Tk()
var = IntVar()
for i in range(10):
Checkbutton(root, text="Option "+str(i), variable = var).pack()
root.mainloop()
To resolve this you need to use a different variable for each Checkbutton, like the below:
from tkinter import *
root = Tk()
var = []
for i in range(10):
var.append(IntVar())
Checkbutton(root, text="Option "+str(i), variable = var[i]).pack()
root.mainloop()
How do I display the output of a MySQL query with TKinter?
Code Snippet:
cursor.execute("SELECT bin FROM stock_lists WHERE part_number = %s", (myvar))
self.myvar=cursor.fetchone()
self.label4 = Label(self, text=0, textvariable=self.myvar)
The snippet code above should display the bin value in the self.label4 widget. But currently the label is not displaying anything. What am I doing wrong?
You have to ways to resolve your problem:
First option:
First, if you want to keep the use of a Tkinter variable as you did, you need to modify its content using the set() method:
#You must declare your Tkinter variable previously in your code:
self.tkinter_variable = StringVar()
# Then modify your last line of code this way:
self.label4 = Label(self, textvariable=self.tkinter_variable.set(self.myvar))
Second option:
An other option is to use the text option correctly and get rid of textvariable option especially if you do not need it to control the labels using a Tkinter variable. This means that you need to change this line:
self.label4 = Label(self, text=0, textvariable=self.myvar)
To:
self.label4 = Label(self, text=self.myvar)
I've been trying to store it as a single string, let alone appending it to a list, by making a variable for it called "Whatisthisthing", but it's not working. Also, do you know why I can't use "Whatisthisthing" to replace Entry.get() with defining Showoncanvas?
import Tkinter
import random
master = Tkinter.Tk()
Entry = Tkinter.Entry()
Entry.pack()
Whatisthisthing = Entry.get()
Canvas = Tkinter.Canvas()
Canvas.pack()
def Showoncanvas(event):
Canvas.create_text(random.randint(10,100), random.randint(10,100), anchor = "center", text=Entry.get())
Entry.bind("<Return>", Showoncanvas)
print Whatisthisthing
master.mainloop()
An entry widget has an textvariable option in which the current text / content is stored. If you use a StringVar as the textvariable the content is automatically synched with this variable and can be read using StringVar's .get() method.
Since I do not have Python 2.7 installed on my system, I converted your code to Python 3 and used mentioned StringVar and its .get() method:
#!/usr/bin/env python3
# coding: utf-8
import tkinter
import random
master = tkinter.Tk()
Whatisthisthing = tkinter.StringVar()
Entry = tkinter.Entry(textvariable=Whatisthisthing)
Entry.pack()
Canvas = tkinter.Canvas()
Canvas.pack()
def Showoncanvas(event):
Canvas.create_text(random.randint(10,100), random.randint(10,100), anchor="center", text=Whatisthisthing.get())
Entry.bind("<Return>", Showoncanvas)
print(Whatisthisthing.get())
master.mainloop()
The only differences between Python 2 and Python 3 should be the following:
Tkinter --> tkinter
print --> print()
I have used xlrd and xlwt earlier where its easy to read rows and column. I am in a process to convert my current excel read and write code(which is currently using xlrd and xlwt) to excel com api.
import win32com.client as win32
excel = win32.Dispatch('Excel.Application')
wbtemp = excel.Workbooks.Open(file path)
readtemp = wbtemp.Worksheets('Temp')
read_1 = readtemp.Cells(1,1)
print read_1
System User
used = readtemp.UsedRange
print used.Find('System User')
System User ## it shows System User is present in worksheet, wont give which cell
print used.Find('xyz')
None ## when its not found on worksheet
Currently above code is hard coded to Cell (1,1), but i am looking for flexible method like search for 'System User' in worksheet and get reply as Cell (1,1).
import win32com.client as win32
excel = win32.Dispatch('Excel.Application')
wbtemp = excel.Workbooks.Open(file path)
readtemp = wbtemp.Worksheets('Temp')
read_1 = readtemp.Cells(1,1)
print read_1
System User
used = readtemp.UsedRange
a = used.Rows.Find('System User')
print a.Address
$A$1 ## Thats what I was looking for..