SAP Gui Script not accessing all data - python

I have a python program where i am trying to get all the data from a grid in SAP-gui (va05) and put in a pandas df.
It's working except that it only pulls the first 195 rows from the grid.
while i< countRows:
reqn = session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell").getCellValue(i,"ZZATTN")
session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell").contextMenu
po = session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell").getCellValue(i,"VBELN")
session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell").contextMenu
I have tried to access the 200th item and just get blanks.
I'm thinking it is a limitation rather than coding. Any thoughts? Thanks!

added the following and it worked.
session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell").firstVisibleRow = i
thinking that it can't read too far past what is visible. this scrolls down the viewable data.

Related

Using python code to set stimuli to certain locations from excel conditions file

I am a final year undergraduate doing my dissertation. I am replicating Reagh's (2016) Mnemonic Similarity Task and am struggling with coding for the Spatial Task.
What I want to achieve is all stimuli coded '5' in my excel file to appear on the left hand side of the screen and all stimuli coded '10' to appear on the right. I've built the rest of my study via builderview and have tried multiple ways of coding this but haven't been successful. Sorry if this is a silly question but am very new to this and have watched and read many things to try and help me - any help at all would be appreciated :) I've added a link to a photo of my excel conditions file.
SpatialStudy.csv
If you have the data in a pandas dataframe, you can use the pivot method with columns='LeftRight' to reorganize the data so there is a column for each unique value within the Left right column.
For reading the file you could use pandas
and for printing a write should do
import pandas as pd
#reads the csv
df = pd.read_csv("yourfile")
#gets all of the ones with 5 on the leftright column
right = df.loc[df['LeftRight'] == 5]
#gets all of the ones with 10 on the leftright column
left = df.loc[df['LeftRight'] == 10]
# for the prinring part i dont know if you mean on the right of the output page or you want a black screen so this is just for the output page
sys.stdout.write("%-6s %-50s %-25s\n" % (right, left))
Insert a code component (from the "custom" component panel in the Builder interface). In its "Begin experiment" tab, insert something like this to map your variable's contents to a position on screen:
position_map = {5:-200, 10:200}
i.e. we have created a dictionary that will return the corresponding horizontal position when fed a value of either 5 or 10. Note that I've used arbitrary positions of -200 and 200 pixels. Replace with whatever values make sense for your stimulus and chosen units.
Then in the relevant stimulus component, put something like this in its "position" field, and set that field to update every repeat:
(position_map[LeftRight], 0)
PS in the future you might find it better to take queries like this to the dedicated support forum at https://discourse.psychopy.org. StackOverflow can be quite fussy about questions that aren't really programming-based.

Pick From Drop-Down List Using XLwings in Python

I have an Excel sheet with a few cells locked with data validation using a drop-down list. I want to give a value to those cells using XLwings. I have seen some similar questions, but nothing solves my issue.
I tried with:
app = xw.App(visible=True)
wb = app.books.open(copy_file)
sht = wb.sheets['Sheet1']
list = sht.range('C21').api.Validation.Formula1[1:]
Honestly I don't know how that last line is supposed to work, I found it at https://github.com/ZoomerAnalytics/xlwings/issues/901.
When I try to run it, it just throws an error in xlwindows.py and stops the code.
Anyone can help?
I can't be sure, but after looking at the link you posted, I think the problem is that you're trying to set a range (the [1:] part means from index 1 to the end, not a single value). In the example from the link, it is asking for those values to be returned, so that was OK. But you're trying to set the value, and you can't set a drop-down menu to an entire range. Delete the ':' and you might have more success.

Editing data in EXCEL sheet using python

I have a excel with multiple columns which contains address fields. I want that address line to be in proper case. When I am able to get them in proper case, some words like 1st,2nd,SW(South West),10th etc., are transforming into 1St, 2Nd, Sw, 10Th. I need python code to resolve this.
addr_df['ADDRESS1'] = addr_df.apply(set_propercase_fn,args=("Address1",), axis=1)
with the above code I am able to get the data in proper case. I tried using below code to make possible changes, It did work but not appropriate.
def replacestring(val):
reps = {'Parker':'Borker', '1St':'st', 'Sw':'SW', 'S W':'SW'}
for i,j in reps.items():
if i in val: val = val.replace(i,j)
return val
print(addr_df['ADDRESS1'].apply(replacestring))
You can try openpyxl it is a great tool, been using it for a long time. With this tool you can modify each and every cell of an excel file.

Sorting and auto filtering Excel with openpyxl

I am trying to sort a spreadsheet using openpyxl and Python. I have read the documents and I don't quite understand this page. I am expecting it to either add the auto filter dropdown arrows or sort my spreadsheet and it is returning errors. Here's my code
wb = openpyxl.load_workbook('report.xlsx')
ws = wb.active
ws['A2'] = "Store"
ws['B2'] = "Manager"
ws['C2'] = "Zone"
ws.column_dimensions.group('F','DU',hidden=True)
#ws.AutoFilter.add_sort_condition('C:C')
wb.save("report.xlsx")
According to the documents it looks like the line "ws.AutoFilter.add_sort_condition('C:C')" should give me the result I want. (Yes I understand it is currently a comment line. The rest of my code runs fine without that line so I commented it.)
When I have that line in the code I get the error - 'Worksheet' object has no attribute 'AutoFilter' but according to the documents it looks like it does. http://openpyxl.readthedocs.org/en/latest/_modules/openpyxl/worksheet/filters.html#AutoFilter.
If anyone can help explain to me why it is failing or what the documents mean that would be great.
This statement in the documents is particularly confusing to me:
"Don't create auto filters by yourself. It is created by :class:~openpyxl.worksheet.Worksheet.
You can use via :attr:~~openpyxl.worksheet.Worksheet.auto_filter attribute."
because I tried that too and it also failed.
Update: #crussell's reply worked in that it added the auto filter to my spreadsheet. However, it is still not adding the sort condition to the appropriate column.
See here: http://openpyxl.readthedocs.org/en/latest/api/openpyxl.worksheet.html?highlight=auto_filter#openpyxl.worksheet.worksheet.Worksheet.auto_filter
The auto_filter command returns the AutoFilter object, so in a sense they are the same thing.
What you need is ws.auto_filter.ref = 'C1:C20'
with the range of cells those of which you want to filter.
According to the documentation openpyxl can define the filter and/or sort but does not apply them!
They can only be applied from within Excel
I don't have a full answer for this, but I did find something interesting when using filters.
I added a filter column,
eg:
ws.auto_filter.add_filter_column(1,{},True)
then I opened the resulting spreadsheet. The column showed the filter! But the data was not actually filtered in the spreadsheet. I had to click on the "Data" tab and click "Reapply" the filter.
So it looks like the adding of a sort or filter column works, except it never actually applies the sort or filter.
I have been hunting down a function that will apply the filter, but have not yet had any luck. If anyone has thoughts, I'd love to hear them!
Thanks!
After looking at the documentation and the source code for the AutoFilter.add_sort_condition() function it looks like the ref you're providing may need to be changed to include row indices, like "C1:C120", for example. Have you tried it with specific row numbers? Also, be sure to take a look at the comment regarding the ref variable right below the function declaration in:
http://openpyxl.readthedocs.org/en/latest/_modules/openpyxl/worksheet/filters.html#AutoFilter
if you're not following where I'm coming from. Cheers and good luck!

XlDirectionDown and selecting filled cells with Python

I've already asked the root question but I thought I might see if I can get more help with this. I'm trying to work with XlDirectionDown in order to select the last filled cell in an Excel spreadsheet.
Ultimately, I'd like to use Python to select all filled cells in this sheet from A through AE. It will be copied into a text file and appended into SQL Server...so I don't want any blanks.
What I have so far:
import win32com.client as win32
excel = win32.gencache.EnsureDispatch('Excel.Application')
excel.Visible = 1;
excel.Workbooks.Open('G:/working.xlsx')
XlDirectionDown = 4
last = excel.Range("A:A").End(XlDirectionDown)
excel.Range("A1:A"+str(last)).Select()
First of all, the XlDirectionDown does not seem to work. The cursor in Excel remains on the first cell.
Secondly, I get an exception for the last line in this code (something to do with Range). Does anybody understand what's going on with this code? Also, is there ANY documentation on win32com or Pywin32 out there?? I can't find any how-to's! Thanks as always everyone.
I have used a specific cell rather than range of cells as starting point. Replace
last = excel.Range("A:A").End(XlDirectionDown)
with
last = excel.Range("A1:A1").End(XlDirectionDown)
However if there are any blank cells, this will stop just before it. You probably want to use UsedRange() instead. This will be the smallest range that contains all your cells, according to Excel: you may find (as I have) that resulting range is wider than AE (contains blank columns at end), and contains many entirely blank rows at the bottom. However, since you want to filter out blank cells anyways, those will be skipped during filtering.
As to the exception on last line of code, this is because End returns a Range object, and you can't convert a range to a string, or if you can then str(last) is a range so "A1:A"+str(last) will be an invalid range.
As to filtering out blank cells, I'm not sure what that means: when you copy the data to a text file, what will you put for blank cells? If you have "A blank C" will you put "A C"? The C will end up in wrong column of your database. Anyways just something that caught my attention.
There is no single place for documentation for win32com, although the Python on Windows book has a lot of info, and google gets you results quite useful, including SO hits. The one thing that keeps tripping me whenever I use Excel COM (this is not specific to python's win32com) is that everything in a workbook is a Range, you can't have an individual cells, even when some methods or properties might lead you to think you are getting a cell you're actually getting a range, it often requires a bit of extra thinking about how to go about getting to the desired cell.
I got started with win32com and Excel here.
In your code, what does excel.Range("A:A").End(XlDirectionDown) return? Test it. You might want to add .Select(), and then use excel.Selection.Address to get the last cell. Test it in interactive mode, it's easier to see what's going on there.
As an alternative, you can use a while loop to go through your cells. This code is looping the rows until an empty cell:
excel.Range("A1").Select()
while excel.ActiveCell.Value:
val = excel.ActiveCell.Value
print(val)
excel.ActiveCell.Offset(2,1).Select() # Move a row down
The last line is a bit funny; in VBA you should write Offset(1,0) to go one row down. However in Python you have to add one to both row and column. Maybe due to indexing?

Categories

Resources