Rename active excel workbook and sheet in python - xlwings

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"

Related

Openpyxl Django

Trying to automate modifying an Ecxel file using openpyxl lib and below code works:
def modify_excel_view(request):
wb = openpyxl.load_workbook('C:\\my_dir\\filename.xlsx')
sh1 = wb['Sheet1']
row = sh1.max_row
column = sh1.max_column
for i in range(2, row + 1):
# ...some_code...
wb.save('C:\\my_dir\\new_filename.xlsx')
return render(request, 'my_app/convert.html', {'wb': wb})
But how to implement a similar method without static (hardcoding) path to file and a filename? Like choosing xlsx file from modal window?
Saving modified copy on a desktop by default? (Maybe like 'ReportLab library': response.write(pdf)=>return response. And it saving on the desktop by default.)
Thank you in advance!
You can send the file path and name as POST parameters and extract them in the views.py function using request.POST.get('file_path').
Just add a line to save the copy to the desktop before returning.

xlwings activate variable sheet name

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, ...)

pygsheets set_dataframe not being recognized

I am trying to write a pandas data frame to a google sheet
# open google sheet where 'test' is the name of the project
sh = gc.open_all('test')
# update the first sheet with df, starting at cell B2 and second sheet with ds
wks = sh[:-1]
wks.set_dataframe(df, (1, 1))
every time I run this app I get this error:
wks.set_dataframe(df, (1, 1))
AttributeError: 'list' object has no attribute 'set_dataframe'
It seems as if set_dataframe is not being recognized within pygsheets
Has anyone encountered this error or know what is the problem?
use gspread_dataframe
you can check it out from here gspread-dataframe

Read Excel worksheet using COM API by Python

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..

Saving an OptionMenu selection to a variable in Tkinter for python 2.7

I am making a GUI in Tkinter that has the user input information and writes it to an excel via openpyxl. I have a few OptionMenu widgets in there and I want to be able to save whatever the user selects from the dropdown menu to a variable that I can then write to a file.
Here is what I have so far:
self.equipment = StringVar(top)
self.equipment.set("10077")
self.e18 = OptionMenu(top, self.equipment,'10077','G2143','G2145','17727')
self.e18.grid(row=13, column=1, sticky=E+W)
Later, in another function I assign it to a variable:
equipment = self.equipment
I then write it to a file:
ws1['D18'] = str(equipment)
When I open the file, instead of the user selected string showing up in the cell, this does: PY_VAR16
All the other information entered in Entryboxes write to excel perfectly. Anybody know how to save the OptionMenu selection to a string so that it will write to excel? Is there an equivalent of a .get() command for this widget? Thanks in advance
Is there an equivalent of a .get() command for this widget?
According to this page, StringVars do indeed have a get method. I suggest replacing
ws1['D18'] = str(equipment)
With
ws1['D18'] = equipment.get()