How do I use OpenPyXL for a specified range? - python-3.x

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.

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 :

How to get a similar sheet name in pandas

I am trying to find a similar sheet name in an excel using pandas.
Currently I am using below code to get dataframe of a sheet in pandas.
excel= pd.ExcelFile(excel)
tab_name = 'Employee'
emp_df= excel.parse(tab_name)
But this code will fail if the sheet name in excel contains any space or some other extra characters.
Is there any easy way to do this ?
I used similarity api (fuzzywuzzy) to find similar sheet only when sheet not found error thrown when running excel.parse(tab_name)
from fuzzywuzzy import fuzz
import xlrd
try:
tab_df = excel.parse(tab_name)
except xlrd.biffh.XLRDError:
sheet_names=excel.sheet_names
ratios = [fuzz.ratio(tab_name, tbname) for tbname in sheet_names]
if(max(ratios)>50):
tab_name = sheet_names[ratios.index(max(ratios))]
tab_df = excel.parse(tab_name)
else:
logger.error(tab_name+"Not found")

write a list to Excel, starting at a specific cell, with openpyxl

I am trying to write a list of values to an Excel spreadsheet, starting at a specific cell. The values will be written periodically and the new values will replace the existing values, so writing will always start in cell F2. I have tried multiple versions that I found on SO and other sites but I keep getting various errors, most recently a KeyError = 0 for these efforts :
for rowNum in range(len(list)):
ws.cell(row=rowNum+1, column=5).value = list[rowNum]
for i in range(len(list)):
ws['F' + r].value = list[i+1]
PLEASE help ! Many thanks in advance.
Edit - I found the solution in "Automate the Boring Stuff with Python", chapter 12. I converted my dataframe to a dictionary and then this worked :
for rowNum in range(2, ws.max_row):
item = ws.cell(row=rowNum, column=1).value
if item in new_dict:
ws.cell(row=rowNum, column=5).value = new_dict[item]
I just tried the script below and it worked fine for me.
from openpyxl import Workbook
wb = Workbook()
# grab the active worksheet
ws = wb.active
# Data can be assigned directly to cells
ws['A1'] = 42
# Rows can also be appended
ws.append([1, 2, 3])
# Python types will automatically be converted
import datetime
ws['A2'] = datetime.datetime.now()
# Save the file
wb.save("C:\\Users\\your_path_here\\Desktop\\sample.xlsx")

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.

Xlwings - adding columns

I am using xlwings and am trying to sum two cells, A1 + B1, and have the output entered into a third cell C1. I want to to do this for a range (A1->A9) + (B1->B9) and have them written to cells (C1->C9). My current command is Range('C3').value = [sum(x) for x in zip(Range('A3:A9').value, Range('B3:B9').value)] This sums the values correctly but the answers are written out horizontally to cells C3-I3. I know that the command Range('A1').value = [[1],[2],[3],[4],[5]] would list the elements of a list vertically but I am not sure how to incorporate this into my command.
Any help would be appreciated
Making this easier is an open issue. For simple lists, you can do the following (note that on Python 2, you don't need list around zip:
Range('C3').value = list(zip([sum(x) for x in zip(Range('A3:A9').value,
Range('B3:B9').value)]))
If you have numpy installed, the same can be achieved like that:
import numpy as np
Range('C3').value = (Range('A3:A9', asarray=True).value +
Range('B3:B9', asarray=True).value)[:,np.newaxis]
Try this code:
import xlwings as xw
sht = xw.Book().sheets[0]
sht.range('C3').options(transpose=True).value \
=[sum(x) for x in zip(sht.range('A3:A9').value, sht.range('B3:B9').value)]
To make values print out in Excel vertically you simply need to covert them in lists, to get list of lists finally, just as you described.
from xlwings import Workbook, Range
wb = Workbook('example.xlsx')
a_col = Range('A1').vertical.value # or Range('A1:A9').value if you wish
b_col = Range('B1').vertical.value
c_col = zip(a_col, b_col)
c_col = [[sum(x)] for x in c_col] # use '[]' here inside of list constructor
Range('C1').value = c_col
wb.close()

Resources