I come from Matlab environment, and recently, trying to dive into python. Reading from excel is one of the more important day to day thing which Matlab accomplishes by creating a COM excel component, deriving workbook and then sheet and then range from it. Was glad to see similar approach from workbook to sheet to cell/range.
So,
w = openpyxl.load_workbook('D:/ass.csv')
s = w.get_sheet_by_name('Sheet1')
c = s.cell('a2')
and finally: c.value
should give me the value, and c.value = 12
should assign the value. Both don't seem to happen. However, once I assign c.value = 12
, then further calling of c.value gives me 12, though no change has happened in the underlying csv.
When I try stuff like s.get_highest_column()
I get the right expected answer. w.get_sheet_names()
also gives the expected response. Any suggestions as to what could have gone wrong.
openpyxl
only works on Excel 2007 and later formats, the files normally having an extension .xlsx
or .xlsm
.
If you want to read the old .csv
format use the CVS module.
AFAIK there is no Python library that will open both the .csv
and the more modern .xls
resp. .xlsx
files. If you want to have that flexibility you can interface with Excel using COM from Python as well. I mostly use win32com.client
for that, but you can probably use comtypes.client
as well.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.