使用openpyxl打开xlsx时出现问题

时间:2016-08-24 16:31:18

标签: python xlsx openpyxl

我有一个xlsx文件,我尝试使用openpyxl加载此文件

from openpyxl import load_workbook 
wb = load_workbook('/home/file_path/file.xlsx')

但是我收到了这个错误:

"wb = load_workbook(new_file)"): expected string or buffer

new_file是一个变量,xlsx文件的路径试图打开。有谁知道为什么会这样,或者我应该如何改变以阅读文件?谢谢!

更新有关错误的更多详细信息

/home/vagrant/scrapy/local/lib/python2.7/site-packages/openpyxl/reader/worksheet.py:322: UserWarning: Unknown extension is not supported and will be removed
  warn(msg)
/home/vagrant/scrapy/local/lib/python2.7/site-packages/openpyxl/reader/worksheet.py:322: UserWarning: Conditional Formatting extension is not supported and will be removed
  warn(msg)
Traceback (most recent call last):
  File "/vagrant/vagrant_conf/pycharm-debug.egg/pydevd_comm.py", line 1071, in doIt
    result = pydevd_vars.evaluateExpression(self.thread_id, self.frame_id, self.expression, self.doExec)
  File "/vagrant/vagrant_conf/pycharm-debug.egg/pydevd_vars.py", line 344, in evaluateExpression
    Exec(expression, updated_globals, frame.f_locals)
  File "/vagrant/vagrant_conf/pycharm-debug.egg/pydevd_exec.py", line 3, in Exec
    exec exp in global_vars, local_vars
  File "<string>", line 1, in <module>
  File "/home/vagrant/scrapy/local/lib/python2.7/site-packages/openpyxl/reader/excel.py", line 252, in load_workbook
    wb._named_ranges = list(read_named_ranges(archive.read(ARC_WORKBOOK), wb))
  File "/home/vagrant/scrapy/local/lib/python2.7/site-packages/openpyxl/workbook/names/named_range.py", line 130, in read_named_ranges
    if external_range(node_text):
  File "/home/vagrant/scrapy/local/lib/python2.7/site-packages/openpyxl/workbook/names/named_range.py", line 112, in external_range
    m = EXTERNAL_RE.match(range_string)
TypeError: expected string or buffer

1 个答案:

答案 0 :(得分:0)

语法是:

wb = load_workbook(filename='file.xlsx', read_only=True)

不需要read_only关键字。