Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get value of specific cells with openpyxl

I have the following Openpyxl code. This code gets the value of every cell in the range. I however only want the values from columns A,B,C,M,W and X for each row. How on earth do I do this? Thank you all in advance.

from openpyxl import load_workbook
wb = load_workbook('C:\\Users\\RileyJ\\Documents\\Luci\\ASquad.xlsx', read_only=True, data_only=True)
ws = wb.get_active_sheet()
ws_ranges = ws['A4:X45']

for row in ws_ranges:
    html_data += "\t\t\t<tr>\n"
    for cell in row:
        if cell.value is None:
            html_data += "\t\t\t\t<td>" + ' ' + "</td>\n"
        else:
            html_data += "\t\t\t\t<td>" + str(cell.value) + "</td>\n"
    html_data += "\t\t\t</tr>\n"
html_data += "\t\t</table>\n"
html_data += "\t</body>\n"
html_data += "</html>"

with open("C:\\Users\\RileyJ\\Documents\\Luci\\Luci.html", "w") as html_fil:
    html_fil.write(html_data)
like image 483
Jeff Riley Avatar asked Feb 11 '23 15:02

Jeff Riley


1 Answers

for row in range(4, 46):
    for column in "ABCMWX":  
        cell_name = "{}{}".format(column, row)
        print ws[cell_name].value
like image 119
7stud Avatar answered Feb 23 '23 14:02

7stud