Posts

Showing posts with the label openpyxl

How to assign a variable name to a worksheet in openpyxl?

Image
Clash Royale CLAN TAG #URR8PPP How to assign a variable name to a worksheet in openpyxl? In openpyxl 2.4.8, is it possible to give a created Excel worksheet a name that depends on user input, for example? What I tried so far is the most obvious thing from openpyxl import load_workbook ... wb=load_workbook("Some_WB_name.xlsx") ws=wb.create_sheet("Data_set_", int(some_user_input)) but it gives me a worksheet called just "Data_set_". I also checked the docs https://openpyxl.readthedocs.io/en/2.5/index.html but did not find anything related there. Thanks in advance. 1 Answer 1 from this PDF documentation, the create_sheet method has two optional arguments: create_sheet create_sheet(title=None, index=None) create_sheet(title=None, index=None) This method will create a worksheet (at an optional index). Parameters Your user input is being passed as the second parameter ( inde...

Incorrect iteration

Image
Clash Royale CLAN TAG #URR8PPP Incorrect iteration I'm cycling through several URLs. For each URL, I want to check to see if the webname has the name of an animal in the URL string. If so, store "Animal Website" in excel row. If not, store "Regular Website" in excel row. Currently, I am not getting the results I as expected. I am getting all Regular Website stored in each row of excel, even though the first iteration has an animal name. Here are the websites (they are fake - only for illustration purposes): www.cats.com www.plants.com www.cars.com www.planes.com The first iteration has an animal's name, therefore I should get that name stored in excel sheet, row 1. The second, third, and forth iteration does not have an animal and thus those names should be stored on excel sheet, row 2, 3, and 4. import openpyxl wb = openpyxl.load_workbook('/path/filename.xlsx') sheet = wb.get_sheet_by_name('Sheet') for url in urls: website_name = url ...