Openpyxl: How to merge cells using variable rows - python-3.x

I can't figure out how to merge cells without using the format 'A1:A4'
I want to be able to use ws.cell(row=x,column=y) format to set the start and end points of the merge.
The code below demonstrates what I want, but doesn't work as the range argument isn't in the correct format.
for x in range(10):
start = ws.cell(row=x,column=1)
end = ws.cell(row=x, column=4)
ws.merge_cells(start:end)
Any help much appreciated!

See the manual, we have the following:
merge_cells(range_string=None, start_row=None, start_column=None, end_row=None, end_column=None)
so what you need to do is to do this instead:
for x in range(10):
ws.merge_cells(start_row=x, start_column=1, end_row=x, end_column=4)

Related

Dynamically set the parameter (value_if_true) of an IF formula

I am working with large Excel stocks data. I have the data in a format like this,
What I need to do is, I need to set that stock ticker name in front of the cell which loss is less than -10%.
I can try with the simple =IF(B2<-0.1, "AAL", "") formula, but this will work until the next stock starts, I mean in AADI also it will print "AAL", that's the problem. I need to print the right ticker if this condition is true. If it's AAPL, the ticker AAPL should print in front of the loss cell. So, How can I do that?
Don't know how to complete this while I am having millions of data points. I should know a good solution using Python, VB, or Excel formulas.
IIUC, here is a simple proposition using openpyxl :
from openpyxl import load_workbook
wb = load_workbook("file.xlsx")
ws = wb['Sheet1']
for num_row in range(1, ws.max_row+1):
cellB = ws.cell(row=num_row, column=2)
if isinstance(cellB.value, str):
ticker_name = cellB.value
else:
try:
cellC = ws.cell(row=num_row, column=3)
if cellC.value < 0.1:
ws.cell(row=num_row, column=4).value = ticker_name
except TypeError:
pass
wb.save("file.xlsx")
NB: Make sure to keep always a backup/copy of your original Excel file before running any kind of python/openpyxl's script.
# Output :

Python xlwings-VBA issue

I'm new to python-excel automation. I have made a script that work properly when run from python, but when I am trying to run it from excel, I get some errors.
The script is something like:
sheet1=pd.read_excel("xxx.xlsx",sheet_name="A", header=1,usecols=["d","e","f","m","n"]))
sheet1.dropna(subset=["f"], inplace=True)
sheet1=sheet1[~sheet1["f"].astype(str).str.startswith("A")]
column_name=list(sheet1["f"].unique())
def fct1():
for index in range(len(column_name)):
for element in sheet1.index:
*do some things and append the results in a dictionary*
ws = xw.Book('xxx.xlsx').sheets['B']
for i in range(len(column_name)):
*write the results*
In VBA I just added:
Sub test()
RunPython "import RoomData; RoomData.fct1()"
End Sub
Could you please help me with this issue?
Found a workaround (not sure if it is the best option)- I created the dataframe by another method, and after I discarded the columns that were not important + the rest is the same:
sheet = xw.Book('xxx.xlsm').sheets['A'].range((3, 4), (1000, 12)).value
sheet1 = pd.DataFrame(sheet, columns=xw.Book('xxx.xlsm').sheets['A'].range((2,4), (2, 12)).value)
sheet1.pop('D')
sheet1.pop('E')
sheet1.pop('H')
sheet1.pop('G')
sheet1.dropna(subset=["f"], inplace=True) # delete lines where no info on "Tableau"
sheet1 = sheet1[~sheet1["f"].astype(str).str.startswith("A")]
column_name = list(sheet1["f"].unique())
def fct1():...
def fct2():...
def fct3():...
Still, if somebody have any idea to do this in some other way, please don't hesitate :)

How do I use OpenPyXL for a specified range?

How do I divide each value in one column by each value in a separate column?
Do I use the range function?
Example:
for i in range(2,80):
sheet['D{}'.format(i)] = '=C1/E1, C2/E2, C3/E3, etc...'
You can get it done by applying division operations to the actual values of the cells. Your code is pretty close; you just need to correct the right hand side by accessing the cell values:
import openpyxl
wb = openpyxl.load_workbook('path/to/xl/file', read_only = False)
# Assuming you are working with Sheet1
sheet = wb['Sheet1']
for i in range(2,80):
try:
sheet['D{}'.format(i)].value = int(sheet['C{}'.format(i)].value)/int(sheet['E{}'.format(i)].value)
except ValueError:
print("{} and/or {} could not be converted to int.".format(sheet['C{}'.format(i)].value, sheet['E{}'.format(i)].value))
wb.save('path/to/new/xl/file')
I hope this helps.

Boolean, Flatnonzero, Selecting a certain range in numpy in python

I have a data file in .txt that consists of 2 columns. The first one is my x values and the second column contains my y values.
What I am trying to do is quite simple. I want to identify where my x values are =>1700 and <=1735 so that I can get the repective y values within that x range. At the end I want to get the sum of those y values.
The following is the code I wrote.
import numpy as np
data = np.loadtxt('NI2_2.txt')
x_all= data[:,0]
y_all= data[:,1]
x_selected= np.flatnonzero(np.logical_and(x_all<=1700),(x_all=>1735))
y_selected= y_all[x_selected]
y_final= np.sum(y_selected)
I get an error message for my x_selected, saying that the syntax is not correct. Does someone see what is wrong with it?
Thanks!
Cece
Try using np.where:
y_selected = y_all[np.where((x_all >= 1700) & (x_all <= 1735))]
y_final = np.sum(y_selected)
EDIT:
Also you cannot write => in python. Use >=.
It may be only because the comparison operand is >= and not => but i can't try any further, sorry.

How to execute the second Iterarion of Data in excel using Openpyxl with Python 3.4

I am trying to read data from my Excel spreadsheet and so far i have been able to do it using the code below but i cant run iterations.
from openpyxl import load_workbook
import numpy as np
wb = load_workbook('c:\ExcelData\pyExcel.xlsx')
ws = wb.get_sheet_by_name('Sheet1')
table = np.array([[cell.value for cell in col] for col in ws['A2':'A3']])
print(table)
Another Example:
val1=2
val2=1
wb = load_workbook(os.path.abspath(os.path.join(os.path.dirname(__file__),'c:\ExcelData\pyExcel.xlsx')))
sheet = wb.get_sheet_by_name('Sheet1')
c = sheet.cell(row=val1, column=val2).value
d = sheet.cell(row=val2, column=val2).value
print(c)
print(d)
So far what this does is to read a harcoded row and cell from an excel file and print or assign the value to a variable, But I am looking for a way to run iterations of data.. I want to use it as a data table when the first rows of all the columns will be executed the first time and then at the end the script will start over again but using the next row.
Thanks.
pinky you should use variables into the table = np.array([[cell.value for cell in col] for col in ws['A2':'A3']])
example ws['variable':'variable']]) or ws['ANUMBERVARIABLE':'ANUMBERVARIABLE']])
#Pinky Read this page http://openpyxl.readthedocs.org/en/latest/tutorial.html and try to find your answer. If you still did not understand from it, I'll try to help you with a code. I feel this is the best way you could actually learn what you are doing rather than just receiving the code directly.

Resources