I want to filter using openpyxl.

Asked 1 years ago, Updated 1 years ago, 276 views

What do you want to do

Read data from Excel and select data only if column B is blank
I'd like to search in my browser.

Currently, the code below can be used for processing, but
Since the first line will be loaded, all countries will be searched from the first line.
If column B is blank just before, I would like to search by data from Japan, Brazil.

Excel data:

Country Flag      
America OK
Japan   
Argentina OK
Brazil  

What you want to achieve

I would like to specify a column like pandas and filter it just before.

# remove the filter from OK
 df=df[df["Flag"]!="OK"]
 # print(df)

Alternatively, select column B blank

# Select only spaces
  df = df [df['Flag'].isnull()]

Excel filters:

Country Flag      
Japan   
Brazil  

Excel Results:

Country Flag      
America OK
Japan OK
Argentina OK
Brazil OK

Code

import openpyxl 
import time
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.common.exceptions import NoSuchElementException

# Excel loading
file_excel_r=r "C:\Users\test\Documents\test.xlsx"

# Load Excel file
wb=openpyxl.load_workbook(file_excel_r)
ws = wb ["Sheet1" ]

# Read line by line
# loop from line 2
for i in range(2,ws.max_row+1):
 
 options=Options()
 options.add_argument('--headless') 
 options.add_argument('--no-sandbox')
 options.add_argument('--disable-gpu')
     
 driver=webdriver.Chrome(executable_path=ChromeDriverManager().install())

 #country
 country=ws['a'+str(i)].value

 #flag
 flag = ws ['b'+str(i)].value

 # url specification
 url="https://www.google.com/search?q="+country
 print(url)
 
 # open a search site
 driver.get(url)
 
 # Wait 2 seconds
 time.sleep(2)

 # Write OK if column B is blank
 if flag is None or not str(flag).strip():
        ws ['B'+str(i)].value="OK"
 else:
       print('No Blank')

 # Save Excel
 wb.save(file_excel_r)

 driver.quit()

How can I filter using openpyxl?
Professor, please.

python python3 openpyxl

2022-10-28 10:16

1 Answers

openpyxl does not have the ability to extract targets with flexible column filters such as pandas.
You can set the filter to hide like Excel, but I don't think it's the feature you want.
Consider using conditional branches in if statements instead of filters.

The reason why all countries are searched is that they always call driver.get(url) without conditional branching.
If the #B column is blank, it has already branched the conditions you requested with the if statement stating OK, so if the if conditions below are met, I think you can achieve the goal by rewriting the code so that you do not open the # search site.


2022-10-28 10:16

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.