简体   繁体   English

Openpyxl单元格值不来了

[英]Openpyxl cell value not coming

I come from Matlab environment, and recently, trying to dive into python. 我来自Matlab环境,最近尝试进入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. 从excel读取是Matlab通过创建COM excel组件,先导出工作簿,然后生成工作表,然后从其范围内完成的最重要的日常工作之一。 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. 最后: c.value应该给我值, c.value = 12应该给我值。 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. 但是,一旦我分配c.value = 12 ,则进一步调用c.value会给我12,尽管基础csv没有发生任何变化。

When I try stuff like s.get_highest_column() I get the right expected answer. 当我尝试s.get_highest_column()类的东西时,我得到了正确的预期答案。 w.get_sheet_names() also gives the expected response. w.get_sheet_names()也给出了预期的响应。 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 . openpyxl仅适用于Excel 2007和更高版本的格式,这些文件通常具有扩展名.xlsx.xlsm

If you want to read the old .csv format use the CVS module. 如果要读取旧的.csv格式,请使用CVS模块。

AFAIK there is no Python library that will open both the .csv and the more modern .xls resp. AFAIK没有Python库可以同时打开.csv和更现代的.xls .xlsx files. .xlsx文件。 If you want to have that flexibility you can interface with Excel using COM from Python as well. 如果您希望具有这种灵活性,则也可以使用来自Python的COM与Excel进行交互。 I mostly use win32com.client for that, but you can probably use comtypes.client as well. 我主要使用win32com.client ,但是您也可以使用comtypes.client

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM