csv - How to get the actual value of a cell with openpyxl? -
i'm beginner python , need help. i'm using python 2.7 , i'm trying retrieve cell values of excel file , store csv file. code following:
import os, openpyxl, csv aggname = "deu" wb_source = openpyxl.load_workbook(filename, data_only = true) app_file = open(filename,'a') dest_file = csv.writer(app_file, delimiter=',', lineterminator='\n') calib_sheet = wb_source.get_sheet_by_name('calibration') data = calib_sheet['b78:c88'] data = list(data) print(data) in range(len(data)): dest_file.writerow(data[i]) app_file.close()
in csv file, this, instead of actual value (for example in case: sfcg, 99103).
<cell calibration.b78>,<cell calibration.c78> <cell calibration.b79>,<cell calibration.c79> <cell calibration.b80>,<cell calibration.c80> <cell calibration.b81>,<cell calibration.c81> <cell calibration.b82>,<cell calibration.c82> <cell calibration.b83>,<cell calibration.c83> <cell calibration.b84>,<cell calibration.c84> <cell calibration.b85>,<cell calibration.c85> <cell calibration.b86>,<cell calibration.c86> <cell calibration.b87>,<cell calibration.c87> <cell calibration.b88>,<cell calibration.c88>
i tried set data_only = true, when opening excel file suggested in answers similar questions doesn't solve problem.
---------------edit-------------
taking account first 2 answers got (thank you!), tried several things:
for in range(len(data)): dest_file.writerows(data[i].value)
i error message :
for in range(len(data)): dest_file.writerows(data[i].values) traceback (most recent call last): file "<ipython-input-78-27828c989b39>", line 2, in <module> dest_file.writerows(data[i].values) attributeerror: 'tuple' object has no attribute 'values'
then tried instead:
for in range(len(data)): j in range(2): dest_file.writerow(data[i][j].value)
and have following error message:
for in range(len(data)): j in range(2): dest_file.writerow(data[i][j].value) traceback (most recent call last): file "<ipython-input-80-c571abd7c3ec>", line 3, in <module> dest_file.writerow(data[i][j].value) error: sequence expected
so then, tried this:
import os, openpyxl, csv wb_source = openpyxl.load_workbook(filename, data_only=true) app_file = open(filename,'a') dest_file = csv.writer(app_file, delimiter=',', lineterminator='\n') calib_sheet = wb_source.get_sheet_by_name('calibration') list(calib_sheet.iter_rows('b78:c88')) row in calib_sheet.iter_rows('b78:c88'): cell in row: dest_file.writerow(cell.value)
only error message:
traceback (most recent call last): file "<ipython-input-81-5bed62b45985>", line 12, in <module> dest_file.writerow(cell.value) error: sequence expected
for "sequence expected" error suppose python expects list rather single cell, did this:
import os, openpyxl, csv
wb_source = openpyxl.load_workbook(filename, data_only=true) app_file = open(filename,'a') dest_file = csv.writer(app_file, delimiter=',', lineterminator='\n') calib_sheet = wb_source.get_sheet_by_name('calibration') list(calib_sheet.iter_rows('b78:c88')) row in calib_sheet.iter_rows('b78:c88'): dest_file.writerow(row)
there no error message reference of cell in csv file , changing dest_file.writerow(row.value) brings me tuple error.
i still need help!
you've forgot cell's value! see the documentation
Comments
Post a Comment