Python Xlsx writing format advice - python

I've created a list and a for loop to iterate over each item in it to print it to a cell in excel. I'm using openpyxl. When I first started using it using easy statements like:
sheet["A1"] = "hello"
results in Cell A1 perfectly representing the hello value, without quotation marks.
I have this code:
workbook = Workbook()
sheet = workbook.active
text = ["Whistle", "Groot", "Numbers", "Mulan", "Buddy Holly"]
other = [5, 8, 100, 120]
for i in range(1,len(text)+1):
cell_letter = "A"
cell_number = str(i)
sheet[str((cell_letter + cell_number))] = str(text[i-1:i])
and it writes to the corresponding cell locations with the iterations over the variable "text". But when i open the file the format is ['Whistle'] and ['Groot']
What am I missing? Should I be passing each iteration to another variable to convert it from a list to a tuple for it to be written in then?
Sorry if my code seems a bit messy, I've literally just learned this over the past few hours and it's (kind of) doing what I need it to do, with the exception of the writing format.

Openpyxl let you write a list of lists, where the intern lists represents the 'lines' in a xlsx file.
So, you can store what you want as:
data_to_write = [["Whistle", "Groot", "Numbers", "Mulan", "Buddy Holly"]]
or, if you want some data in the next line:
data_to_write = [["Whistle", "Groot", "Numbers"], ["Mulan", "Buddy Holly"]]
then, add it to your WorkSheet:
for line in data_to_write:
sheet.append(line)
and, finally, save it:
workbook.save("filename.xlsx")
The full code could be something like:
from openpyxl import Workbook
workbook = Workbook()
sheet = workbook.active
data_to_write = [["Whistle", "Groot", "Numbers", "Mulan", "Buddy Holly"]]
for line in data_to_write:
sheet.append(line)
workbook.save('example.xlsx')
Give it a try and, then, give me a feedback, please XD

Related

How to check which one of the excel sheets has a specific word inside using python?

I have been building a project in python and i have been having a little problem when working with python and excel. I have an excel document with 50+ sheets(Sheet1, Sheet2, ...) and I want to find which of the sheets has a word inside them. For example: I am looking for the sheets that have the word "work"(in one of the cells) inside them, and as result have the name of the sheets that have that word inside them(the result can be multiple sheets for this example, like Sheet4, Sheet43, Sheet50). Thank you for reading and for the help.
I tried to find a answer by myself and I failed. Then I tried to find the answer on the internet and most of the posts discus the next problem: finding the sheets that have a specific word in their name. This is not for what I am looking. I am looking for finding the sheets that have a specific word in them(not in the name but in one of the cells). So far I have been using pandas for context.
import pandas as pd
exel_data = pd.read_excel("data.xlsx")
##### converting into comma-separated values
exel_data.to_csv("data.txt")
##### Open in read mode
file = open("ptry.txt", "r")
##### reading comma-separated values
file_str = filex.read()
##### Spliting it on the basis on , (in my case) you can use whatever suit your data type and creating a list
file_list = file_str.split(",")
#### if "hello world is in it return true else false
if "hello world" in file_list:
print("True")
else:
print("false")
from openpyxl import load_workbook
xls = load_workbook(filename= excel_path , data_only=True)
for i in xls.sheetnames:
ws = xls[str(i)]
for num_row in range (1, ws.max_row +1):
# print(ws.max_row)
if ws['A{}'.format(num_row)].value=='work':
print (str(i))
Using sheet_name=None and a list comp:
import pandas as pd
file = "path/to/file/file.xlsx"
search_for = "work"
sheet_mapping = pd.read_excel(file, sheet_name=None).items()
found_in = [sheet for sheet, df in sheet_mapping if search_for in df.values.astype(str)]
print(found_in)

python Get excel sheet names in array, and put it in condition

What I want is load excel sheet which is ("gx_projectid.xlsx") for my example. After then get the sheet names and put them in an array. After then if these sheet names ending with "_ID" I want to separate them. For this code they: [0],[1],[2],[3],[4],[11] and I want to access and work on them later with "wb_obj.worksheets[x]".
import openpyxl
from openpyxl.styles import Font
wb = openpyxl.load_workbook("gx_projectid.xlsx") ## EXCELI YUKLEME
sheet = wb.active
Sheet_Names = [wb.sheetnames]
print("original sheet names:", Sheet_Names)
sheets = []
for row in Sheet_Names:
for cell in row:
sheets.append(cell.split())
print("Put it in an array : ",sheets)
my current output:
original sheet names: [['Reserved_ID', 'PowerLED_ID', 'RC_ID', 'Brand_ID', 'Product_ID', 'Panel', 'EDID', 'Cabinet', 'DEC', 'EnergyClass', 'CompatibleConfig', 'Project_ID', 'Project-id']]
Put it in an array : [['Reserved_ID'], ['PowerLED_ID'], ['RC_ID'], ['Brand_ID'], ['Product_ID'], ['Panel'], ['EDID'], ['Cabinet'], ['DEC'], ['EnergyClass'], ['CompatibleConfig'], ['Project_ID'], ['Project-id']]
idk how to check if the sheet names ends with "_ID" because i tried:
for i in range (len(sheets)):
print("sheet names",[i],": ",sheets[i])
# if sheets[i].endswith("_ID']"):
and I got some error because its list not a string type.
First a small tip, try to name your variables and functions with snake_case format. CamelCase naming format is mainly used for naming classes in python. I reccomend looking up pep8 documents.
Now let's get to the main problem. You are trying to use a str function on a list data type. First you should convert your data to str:
for i in range (len(sheets)):
str_sheet_name = str(sheets[i]) # converting to str
if str_sheet_name.endswith("_ID']"):
print(str_sheet_name)
this should work. please let me know

Printing Python Output to Excel Sheet(s)

For my master thesis I've created a script.
Now I want that output to be printed to an excel sheet - I read that xlwt can do that, but examples I've found only give instructions to manually print one string to the file. Now I started by adding that code:
import xlwt
new_workbook = xlwt.Workbook(encoding='utf-8')
new_sheet=new_workbook.add_sheet("1")
Now I have no clue where to go from there, can you please give me a hint? I'm guessing I need to somehow start a loop where each time it writes to a new line for each iteration it takes, but am not sure where to start. I'd really appreciate a hint, thank you!
since you are using pandas you can use to_excel to do that.
The usage is quite simple :
Just create a dataframe with the values you need into your excel sheet and save it as excel sheet :
import pandas as pd
df = pd.DataFrame(data={
'col1':["output1","output2","output3"],
'col2':["output1.1","output2.2","output3.3"]
})
df.to_excel("excel_name.xlsx",sheet_name="sheet_name",index=False)
What you need is openpyxl: https://openpyxl.readthedocs.io/en/stable/
from openpyxl import Workbook
wb = openpyxl.load_workbook('your_template.xlsx')
sheet = wb.active
sheet.cell(row=4, column=2).value = 'what you wish to write'
wb.save('save_file_name.xlsx')
wb.close()
Lets say you would save every result to a list total_distances like
total_distances = []
for c1, c2 in coords:
# here your code
total_distances.append(total_distance)
and than save it into worksheet as:
with Workbook('total_distances.xlsx') as workbook:
worksheet = workbook.add_worksheet()
data = ["Total_distance"]
row = 0
worksheet.write_row(row,0,data)
for i in total_distances:
row += 1
data = [round(i,2)]
worksheet.write_row(row,0,data)

How do I import multiple excel files with similar sheet names using wild card?

I'm importing this sheet called "My Bench Sheet 1" using the following code.
bench_file_1 = pd.read_excel("Bench1.xlsx", sheet_name = "My Bench Sheet 1")
I have multiple workbooks with sheets starting with "My Bench Sheet". I want to use a wildcard so that I do not have to type the sheet name every time I import it, since it basically is the same except for the figure at the end.
I have tried using this wildcard, but it does not work.
bench_file_1 = pd.read_excel("Bench1.xlsx", sheet_name = "My Bench Sheet*")
Another (but safest) way would be to read the whole excel file first, gather the sheet names. As seen in this explanation: https://stackoverflow.com/a/17977609
excel = pd.ExcelFile("your_excel.xlsx")
excel.sheet_names
# ["Sheet1", "Sheet2"]
dfs = [pd.read_excel("your_excel.xlsx", sheet_name=n) for n in excel.sheet_names]
Could you possibly use f strings.
For example:
for i in range(10):
bench_file_1 = pd.read_excel("Bench1.xlsx", sheet_name = f'My Bench Sheet {i}')
The above example just provides a range from 0-9 inclusive. If you already have that list of numbers, you could iterate through that list instead of generating the numbers from a range.

How to append result of for loop in excel file?

I have a .txt file with a list of keywords, I read this file and for each keyword generate some kind of string. I would like to append this string generated for each keyword to excel file. I'd like also that each time I re run the script and read .txt file with new keywords, result is always appended to the same excel file instead of overwriting it.
I have tried this, but not sure if openpyxl is a good method, also I get an error:
raise ValueError("{0} is not a valid column name".format(str_col))
ValueError: tapis roulant elettrico is not a valid column name
for line
page.append(some_result)
from openpyxl.workbook import Workbook
from openpyxl import load_workbook
headers = ['data']
workbook_name = 'Example.xlsx'
wb = Workbook()
page = wb.active
page.title = 'data'
page.append(headers)
some_result = {}
val = "some result"
with open("keywords.txt", "r") as file:
for line in file:
some_result = {line: val}
page.append(some_result)
wb.save(filename=workbook_name)
file.close()
Just my opinion, its definitely not a good idea to save your file for every entry in your loop. Itll slow things down and overall it will probably break things and make them more complicated. I think it is likely you are saving a new column name on every iteration. I made a few changes/comments that I didnt really test, but hopefully it might help you get moving in the right direction. I'm assuming you just want a single column in your excel sheet with the keywords you mention, but to give you a complete solution I would need to know details about whether youre allowing duplicates, why you need it in excel format at all, and a few other things. If a CSV is acceptable (excel can read these) then there is a much simpler solution than what youre doing if you use numpy and or pandas.
from openpyxl.workbook import Workbook
from openpyxl import load_workbook
headers = ['data']
workbook_name = 'Example.xlsx'
wb = Workbook()
page = wb.active
page.title = 'data'
page.append(headers)
some_result = {}
val = "some result"
temp_page_list = []
with open("keywords.txt", "r") as file:
for line in file:
some_result = {line: val}
#print(some_result)
#dont append to your real excel file here in the loop, doing it in a simple list will be less complicated and faster
#page.append(some_result)
temp_page_list.append(some_result)
#dont save things here
#wb.save(filename=workbook_name)
file.close()
#print some or all of temp_page_list here
#if it looks right, you can perhaps convert the list directly by iterating and saving the elements
#a better option may be to use a built in function from openpyxl to add the contents of temp_page_list if such a function exists
I have not worked with openpyxl before but I want to give you a general understanding of the python code that you wrote.
from openpyxl.workbook import Workbook
from openpyxl import load_workbook
headers = ['data']
workbook_name = 'Example.xlsx'
wb = Workbook()
page = wb.active
page.title = 'data'
page.append(headers)
some_result = {}
val = "some result"
with open("keywords.txt", "r") as file:
for line in file:
# some_result = {line: val} # this is a dictionary
some_result = val + str(line) # this is a string
page.append(some_result)
wb.save(filename=workbook_name)
file.close()
You are trying to append a dictionary with Key being the line variable and the Value associated with this key is the some_result variable. While you are trying to append this Key, Value Pair, I think it is assuming that you want to append the Value in a row that is associated with the Key as the column (but the Key doesn't exist already). So if you try the above code, I think it would append everything under one column. If you want separate column then you need to create columns if they don't exist

Categories