I am importing inputs from excel and I want to check if each input value is within the given range in if condition. but the problem is that the code stop at the row which is a satisfied and print warning and does not take the next rows to check if any other row is also in the range.
# importing openpyxl module
import openpyxl
# workbook object is created
workbook1 = openpyxl.load_workbook("edges.coordinates.xlsx")
workbook2 = openpyxl.load_workbook("sensors data.xlsx")
#For sheet1
sheet_obj1 = workbook1["sheet1"]
#For sheet2
sheet_obj2 = workbook1["sheet2"]
#for sensors sheeta
sheet_obj3 = workbook2["sheet1"]
# Will print a particular row value
for j in range(1, sheet_obj3.max_row):
#Fetching Data From Cell For Sheet1/workbook 2
x= sheet_obj3.cell(row = 1+j, column = 1).value
y = sheet_obj3.cell(row = 1+j, column = 2).value
z = sheet_obj3.cell(row = 1+j, column = 3).value
# Will print a particular row value
for i in range(1, sheet_obj1.max_row):
#Fetching Data From Cell For Sheet1
x1 = sheet_obj1.cell(row = i, column = 1)
y1 = sheet_obj1.cell(row = i, column = 2)
x2 = sheet_obj1.cell(row = i+1, column = 1)
y2 = sheet_obj1.cell(row = i+1, column = 2)
z1 = sheet_obj1.cell(row = i, column = 3)
#Fetching Data From Cell For Sheet2
x11 = sheet_obj2.cell(row = i, column = 1)
y11 = sheet_obj2.cell(row = i, column = 2)
x22 = sheet_obj2.cell(row = i+1, column = 1)
y22 = sheet_obj2.cell(row = i+1, column = 2)
#Printing Warning If Condition Is Satisfied
if z == z1.value:
if min(x1.value,x2.value,x11.value,x22.value)<=x <=max(x1.value,x2.value,x11.value,x22.value) and min(y1.value,y2.value,y11.value,y22.value)<=y<=max(y1.value,y2.value,y11.value,y22.value):
print("Warning Between Row"+str(i)+" and Row"+str((i+1)))2
Related
I use openpyxl and pandas to fill row color with specified condition. Everything works fine but in some cells I lose leading zeros (like 0345 -> output 345), I don't want that. How can I get the exact data?
dt = pd.read_excel(file_luu, sheet_name="Sheet1")
dt = pd.DataFrame(dt)
dinhDanh = len(dt.columns) - 1
wb = load_workbook(file_luu)
print(type(wb))
ws = wb['Sheet1']
for i in range(0, dt.shape[1]):
ws.cell(row=1, column=i + 1).value = dt.columns[i]
for row in range(dt.shape[0]):
for col in range(dt.shape[1] ):
ws.cell(row + 2, col + 1).value = str(dt.iat[row, col]) if (str(dt.iat[row, col]) != "nan") else " "
if dt.iat[row, dinhDanh] == True:
ws.cell(row + 2, col + 1).fill = PatternFill(start_color='FFD970', end_color='FFD970',
fill_type="solid") # used hex code for brown color
ws.delete_cols(1)
ws.delete_cols(dinhDanh)
wb.save(file_luu)
Copy exactly all characters
###Im trying to create a program that checks a column in excel and moves specific rows with specific values to another sheet in the same workbook, but I was wondering if there was a more efficient way to do this using pandas or something else.###
import time
start_time = time.perf_counter ()
import openpyxl
wb = openpyxl.load_workbook("Test.xlsx")
ws=wb.active
mr,mc=ws.max_row,ws.max_column
column_string=input("Enter Column Letter with Email (A or B or C or leave blank to skip editing):").upper()
if len(column_string)>0:
for cell in ws[column_string][1:]:
if cell.value is None:
ws_1=wb.create_sheet('Linkedin Only')
for i in range (1, mr +1):
for j in range (1, mc + 1):
c = ws.cell(row = i, column = j)
ws_1.cell(row = i, column = j).value = c.value
break
for cell in ws_1[column_string][1:]:
if cell.value is not None:
ws_1.delete_rows(cell.row)
for cell in ws[column_string][1:]:
if cell.value is None:
ws.delete_rows(cell.row)
wb.save("Test.xlsx")
else:
wb.save("Test.xlsx")
end_time = time.perf_counter ()
print(end_time - start_time, "seconds")
Per the attached image, I am trying to copy and paste the same data into a different format.
I have figured out the first part of the code but I need help abbreviating the 2nd half after this comment:
"Fills in the concepts per store group step by step"
Currently, this code is not efficient and I would like to have it compressed into just a couple of lines.
Image of desired result (Right hand side):
Here is the code I have cobbled together so far:
import openpyxl as xl;
filename ="c:\\Users\kevin\Documents\Python Programs\Excel Python\Conceptlist.xlsx"
wb1 = xl.load_workbook(filename)
ws1 = wb1.worksheets[0]
# opening the destination excel file
filename1 ="c:\\Users\kevin\Documents\Python Programs\Excel Python\Conceptlist2.xlsx"
wb2 = xl.load_workbook(filename1)
ws2 = wb2.worksheets[0]
# copying the cell values from source
# excel file to destination excel file
rowctsq = ws1['A1']
j = 0
while j < rowctsq.value:
j = j + 3
for i in range (3 , 6):
# reading cell value from source excel file
# Populates the store list repeatedly
c = ws1.cell(row = i, column = 1)
ws2.cell(row =i , column = 1).value = c.value
ws2.cell(row =i + j , column = 1).value = c.value
# Fills in the concepts per store group step by step
c = ws1.cell(row = i, column = 2)
ws2.cell(row =i , column = 3).value = c.value
c = ws1.cell(row = i, column = 3)
ws2.cell(row =i + 3 , column = 3).value = c.value
c = ws1.cell(row = i, column = 4)
ws2.cell(row =i + 6 , column = 3).value = c.value
c = ws1.cell(row = i, column = 5)
ws2.cell(row =i + 9 , column = 3).value = c.value
# saving the destination excel file
wb2.save('c:\\Users\kevin\Documents\Python Programs\Excel Python\Conceptlist2.xlsx')
Hopefully, I get extra community points for answering my own question! I worked through this and have pretty much gotten to my destination. Here's the code I came up with. Works like a charm. :)
import openpyxl as xl;
filename ="c:\\Users\kevin\Documents\Python Programs\Excel Python\Conceptlist.xlsx"
wb1 = xl.load_workbook(filename)
ws1 = wb1.worksheets[0]
# opening the destination excel file
filename1 ="c:\\Users\kevin\Documents\Python Programs\Excel Python\Conceptlist2.xlsx"
wb2 = xl.load_workbook(filename1)
ws2 = wb2.worksheets[0]
# copying the cell values from source
# excel file to destination excel file
rowctsq = ws1['A1']
j = 0
k = 0
while j < rowctsq.value and k < 6:
j = j + 3
k = k + 1
for i in range (3 , 6):
# reading cell value from source excel file
# Populates store column
c = ws1.cell(row = i, column = 1)
ws2.cell(row =i + j , column = 1).value = c.value
# Populates concept 'x' column
c = ws1.cell(row = i, column = 1 + k)
ws2.cell(row =i + j , column = 3).value = c.value
# Populates concept name column
c = ws1.cell(row = 2, column = 1 + k)
ws2.cell(row =i + j , column = 2).value = c.value
# saving the destination excel file
wb2.save('c:\\Users\kevin\Documents\Python Programs\Excel Python\Conceptlist2.xlsx')
ValueError: 2 columns passed, passed data had 4 columns:
import pandas as pd
def customedata():
colnum = input("How many columns do you need? ")
colnum = int(colnum)
rownum = input("How many rows do you need? ")
# user input column and row
rownum = int(rownum)
colName = []
rowName = []
# create an empty list
for col in range(0,colnum):
colValue =input('Enter the value for column name of column %s:' %(col + 1))
colName.append(colValue)
for row in range(0,rownum):
rowValue = (int(input('Enter the value of row number %s:' %(row + 1))))
rowName.append(rowValue)
row = row + 1
col = col + 1
# columns = colName[i]
df1= pd.DataFrame([rowName],columns = colName)
print(df1)
I tried to create a dataframe using user input rows and columns but I keep getting valueError. I tought that it had something wrong with the nested loop but I wasn't able to solve the problem.
I think it would be easier to create a pd.DataFrame from user input using a dictionary like below. First you create an empty dict, then you pass the colName as key and your rowNamelist as value. Then you can use pd.DataFrame.from_dict() to transform your dict to a pd.DataFrame
Hoping it helps :)
def customedata():
colnum = input("How many columns do you need? ")
colnum = int(colnum)
rownum = input("How many rows do you need? ")
# user input column and row
rownum = int(rownum)
colName = []
rowName = []
dictionary = {}
for col in range(0, colnum):
colValue = input('Enter the value for column name of column %s:' % (col + 1))
for row in range(0, rownum):
rowValue = (int(input('Enter the value of row number %s:' % (row + 1))))
rowName.append(rowValue)
dictionary[colValue] = rowName
df1 = pd.DataFrame.from_dict(dictionary)
print(df1)
I want to find the index of all duplicate rows in an excel file and add them to a list which will be handled later.
unwantedRows = []
Row = []
item = ""
for index, row in enumerate(ws1.iter_rows(max_col = 50), start = 1):
for cell in row:
if cell.value:
item += cell.value
if item in Row:
unwantedRows.append(index)
else:
Row.append(item)
However this fails to work. It only indexes rows that are completely empty. How do I fix this?
unwantedRows = []
Rows = []
for index, row in enumerate(ws1.iter_rows(max_col = 50), start = 1):
sublist = []
for cell in row:
sublist.append(cell.value)
if sublist not in Rows:
Rows.append((sublist))
else:
unwantedRows.append(index)
Without a tuple:
row_numbers_to_delete = []
rows_to_keep = []
for row in ws.rows:
working_list = []
for cell in row:
working_list.append(cell.value)
if working_list not in rows_to_keep:
rows_to_keep.append(working_list)
else:
row_numbers_to_delete.append(cell.row)
for row in row_numbers_to_delete:
ws.delete_rows(
idx=row,
amount=1
)