openpyxl returning empty cell values for formula series

这一生的挚爱 提交于 2019-11-30 21:44:09
Felipe

Update 3/23/16

This issue has been resolved in recent versions of openpyxl (2.3, and beyond, I believe) PR request


This is a known shortcoming of openpyxl (bug report). The short answer is that when you drag a formula down or across, Excel uses a "shared formula," which cannot be parsed by openpyxl. If you really need this functionality, you may want to try xlrd.


Edit:

This was bugging me, so here's a very hackish workaround (Tested on Python 2.7). First you have to get Robin Macharg's Excel formula parser from here. Save it somewhere in your Python path as xlparse.py. Now, run the code from this gist, and you should be able to use the get_formula function to get the formula for any cell. In most cases, the function simply returns cell.value, but whenever the function encounters a shared formula, it gives the correct value.

Note: The monkeypatch to openpyxl.reader.worksheet.Worksheet is so that openpyxl.load_workbook uses our patched class. Make sure to run this code before calling load_workbook. If you are creating the workbook a different way, you'll have to patch Worksheet in the appropriate place.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!