Make Excel Cell value Variable in Python Using Pandas - python

I have looked for a while on this one but can't seem to find out how to pick a specific cell value in an excel worksheet and assign it to variable in python. I get a Traceback Error with the code below.
I have a number of work rules I want to assign as variables in python that are stored in an cells within an excel workhseet.
(work rules[4][2] is how I am trying to make the cell value into a variable.
Code:
work_rules = pd.read_excel(
'D:\\Personal Files\\Technical Development\\PycharmProjects\\Call Center Headcount Model\\Call Center Work Rules.xlsx',
sheet_name='Inputs')
historical_start_date = work_rules[4][2]
print(historical_start_date)

Found it:
Use the iloc method on the excel object: work_rules.iloc(4, 2)

Related

How to use Excel defined names in Python to obtain the values?

I have the following set of code which is trying to find the text in a cell within an excel worksheet called 'Input'. I am using openpyxl.
The cell uses a defined name in excel called 'Rperiod'. I can call the text by specifying the sheet and cell range directly, but I'm wondering if there is a way to use the defined_names function to keep it dynamic.
Rperiod = wb.defined_names['Rperiod']
Rperiod.value
This results in 'Input!$F$8' but then I can't workout if it is possible to use this result to get the text. The static method is:
input_sheet = wb.get_sheet_by_name('Input')
Rperiod_cell = input_sheet['F8']
Rperiod_cell.value
This returns the correct result 'Quarterly' but I obviously want to do this without directly specifying the cell 'F8' or the sheet 'Input'.
Any help is greatly appreciated!

How to insert array formula in an Excel sheet with openpyxl?

I'm using OpenPyxl to create and modify an Excel sheet.
I have the following formula in Excel:
=(SUM(IF(LEFT(Balances!$B$2:$B$100,LEN($B4))=$B4,Balances!$D$2:$D$100)))
This formula which is an "array formula" is working but in order to write it by hand, I have to finish with CTRL+SHIFT+ENTER (because it's an array formula).
This transform then the formula as follow:
{=(SUM(IF(LEFT(Balances!$B$2:$B$100,LEN($B4))=$B4,Balances!$D$2:$D$100)))}
I want to be able to write this formula via OpenPyxl with the following code:
sheet.cell(row=j, column=i).value = '{=(SUM(IF(LEFT(Balances!$B$2:$B$100,LEN($B4))=$B4,Balances!$D$2:$D$100)))}'
However, it doesn't work. OpenPyxl can't manage it. It give me the formula written but not working.
I could do it with XLSX Writer
https://xlsxwriter.readthedocs.io/example_array_formula.html
However XLSX writer doesn't work with already created files.
I don't see which path to follow.
Use the worksheet.formula_attributes to set the array formula. Place the formula in the desired cell, A1 for this example. Then set the formula_attributes to the cell range you want to apply the formula to.
ws["A1"] = "=B4:B8"
ws.formula_attributes['A1'] = {'t': 'array', 'ref': "A1:A5"}
In case solution provided above does not work, check whether you are using english name of functions in your formulae.
In my case I have been using czech function name and although formulae works if inserted manually, it did not work when inserted via openpyxl.
Switching to english name of the function solved the issue!
In my case the formula was using arrays for intermediate results before summarizing with a MAX. The formula worked OK when typed in but not when inserted via openpyxl. Office 365 version of Excel was inserting the new implicit intersection operator, #, incorrectly.
formula: ="Y" & MAX(tbl_mcare_opt[Year]*(tbl_mcare_opt[Who]=[#Who])*(tbl_mcare_opt[Year]<=intyear(this_col_name())))
It turns out that the properties needed to be set, as above. This allowed Excel to correctly interpret the formula. In my case the ref turned out to be just the single cell address.
I was able to determine that the formula was using dynamic arrays with a regex. If it was then I added the formula properties.
# provision for dynamic arrays to be included in formulas - notify excel
if is_formula(values[cn]):
regex_column=r'[A-Za-z_]+(\[\[?[ A-Za-z0-9]+\]?\])'
pattern=re.compile(regex_column)
matches=pattern.findall(values[cn])
if len(matches): # looks like a dynamic formula
address=get_column_letter(cix)+str(rix)
ws.formula_attributes[address]={'t':'array','ref': address}

Grabbing the Highest value in Excel Column and display Row Name using Python/Pandas

I'm still fairly new to Python and Pandas and wanted to see if this was possible using pandas. I've read online that the best approach would be using idxmax.
What I'm trying to do:
Search through an excel file called 'Sample' - Check the 'Difference' column and find the highest value --> Grab the 'Metric' name and display into a notepad file/browser. Below, is a picture for reference.
Here is a small snippet that I have seen online, any assistance on how to do this would be appreciated.
Code:
metric_column = Metric[:,1].astype(int)
metric = metric_column[np.argmax(Difference)]
This should do it
df.iloc[df['Difference'].idxmax(), 0]

How to copy a formula from one gsheet to another using python?

I'm not sure if this is possible. I have tons of spreadsheet, and the formulas need to be updated. How do I copy a formula from one cell or a group of cells to another? I've used gspread and it seems it can only do values. I need python to basically paste formulas on hundreds of sheets for me, without me opening each individually and copy and pasting the formulas.
Does anybody have a generic solution for copying and pasting formulas? This is pretty important, you would think someone can do it.
Update 19 July 2018:
Here's how you do it:
# Get the formula value from the souce cell:
formula = wks.acell('A2', value_render_option='FORMULA').value
# Update the target cell with formula:
wks.update_acell('B3', formula)
(this works since gspread 3.0.0 which uses Sheets API v4.)
Original answer below:
To access a formula value, you need to use the input_value attribute of a cell object.
Here's an example. (I'm skipping the initialization step. Let's assume you've already opened a spreadsheet and wks is referring to you worksheet object.)
# To copy a formula from a single cell (say, A2) to another cell (B3)
# use the input_value property
formula = wks.acell('A2').input_value
# then paste the value to a new cell
wks.update_acell('B3', formula)
For a group of cells, you'd want to use a wks.range() method to get cell objects. Then you can get formula values via input_value as in the code above. See the example on the GitHub.

Python - Working with Excel - Enter data everytime 9 cells at a time

I am re writing my question with code, First of all I am new to programming. Started to think about programming recently. :( at very later stage of life :)
My code is as below:
import win32com.client as win32
from win32com.client import Dispatch
excel = win32.gencache.EnsureDispatch('Excel.Application')
wb = excel.Workbooks.Open(r'F:\python\book1.xlsx')
excel.Visible = False
ws = wb.Worksheets("Sheet1")
# to get the last row
used = ws.UsedRange
nrows = used.Row + used.Rows.Count
ws.Cells(nrows,2).Value = "21"
ws.Cells(nrows,2).Offset(2,1).Value = "22"
ws.Cells(nrows,2).Offset(3,1).Value = "23"
#like this nine values
wb.Save()
excel.Application.Quit()
What I am trying to do is write values in the excel sheet.
Old Question Below Ignore it.
I am using Python 2.7 and win32com to access excel file
I am stuck with a problem where I need to enter data in to 9 cells each time on column B
I want to select the last cell in B column and enter the new set of 9 cell values.
I tried to use ws.usedRange but this is not helping as it chooses the last cell wherever the data is present in the whole sheet. You can see in the attached sheet testdata which is spread in columns D,E,F etc so used range chooses the last cell based on that. is there a way to solve my problem? I am ok to use any other module as well if it helps.
A UsedRange:
… includes any cell that has ever been used. For example, if cell A1 contains a value, and then you delete the value, then cell A1 is considered used. In this case, the UsedRange property will return a range that includes cell A1.
Do you want to work on every cell that has ever been used? If not, why would you use UsedRange? If so, what are you trying to use it for? To find the last row in the UsedRange? You can do that easily. The Range Objects docs show you what you can do with them.
Then, once you know what you want to specify, the same documentation shows how to ask for it. You want B10:B18? Just ws.Range('B10:B18').
Once you have that Range object, you can assign a value or formula to the whole range, iterate over its cells, etc. Again, the same docs show how to do it.

Categories