openpyxl.load_workbook(path) read all column as string
- User
openpyxl.load_workbook(path) read all column as string
- Assistant
When you use
openpyxl.load_workbook(path)to load an Excel file, the cell values are read with their native data types (numbers as numbers, strings as strings, dates asdatetimeobjects, etc.).If you want to read all cell values in all columns (or a specific worksheet) as strings, you will need to convert the values to strings yourself after loading the workbook.
Here is a way to do that:
from openpyxl import load_workbook wb = load_workbook(path) ws = wb.active # or wb['SheetName'] data_as_strings = [] for row in ws.iter_rows(values_only=True): # Convert all cell values in the row to strings, handle None cells gracefully row_str = [str(cell) if cell is not None else '' for cell in row] data_as_strings.append(row_str) # data_as_strings is now a list of lists with all values as strings for r in data_as_strings: print(r)