在python

时间:2017-08-08 19:19:04

标签: python openpyxl xlrd

我正在尝试打开由另一个系统创建的xlsx文件(这是数据始终存在的格式,并且不在我的控制之下)。我尝试了openpyxl(v2.3.2)和xlrd(v1.0.0)(以及pandas(v0.20.1)read_excel和pd.ExcelFile(),两者都使用xlrd,所以可能没有实际意义),我我遇到了错误;再加上没有从我的搜索中找到答案。任何帮助表示赞赏。

xlrd代码:

import xlrd
workbook = xlrd.open_workbook(r'C:/Temp/Data.xlsx')

错误:

Traceback (most recent call last):

  File "<ipython-input-3-9e5d87f720d0>", line 2, in <module>
    workbook = xlrd.open_workbook(r'C:/Temp/Data.xlsx')

  File "C:\Program Files\Anaconda3\lib\site-packages\xlrd\__init__.py", line 422, in open_workbook
    ragged_rows=ragged_rows,

  File "C:\Program Files\Anaconda3\lib\site-packages\xlrd\xlsx.py", line 833, in open_workbook_2007_xml
    x12sheet.process_stream(zflo, heading)

  File "C:\Program Files\Anaconda3\lib\site-packages\xlrd\xlsx.py", line 548, in own_process_stream
    self_do_row(elem)

  File "C:\Program Files\Anaconda3\lib\site-packages\xlrd\xlsx.py", line 685, in do_row
    self.sheet.put_cell(rowx, colx, None, float(tvalue), xf_index)

ValueError: could not convert string to float: 

openpyxl代码:

import openpyxl
wb = openpyxl.load_workbook(r'C:/Temp/Data.xlsx')

错误:

Traceback (most recent call last):

  File "<ipython-input-2-6083ad2bc875>", line 1, in <module>
    wb = openpyxl.load_workbook(r'C:/Temp/Data.xlsx')

  File "C:\Program Files\Anaconda3\lib\site-packages\openpyxl\reader\excel.py", line 234, in load_workbook
    parser.parse()

  File "C:\Program Files\Anaconda3\lib\site-packages\openpyxl\reader\worksheet.py", line 106, in parse
    dispatcher[tag_name](element)

  File "C:\Program Files\Anaconda3\lib\site-packages\openpyxl\reader\worksheet.py", line 243, in parse_row_dimensions
    self.parse_cell(cell)

  File "C:\Program Files\Anaconda3\lib\site-packages\openpyxl\reader\worksheet.py", line 188, in parse_cell
    value = _cast_number(value)

  File "C:\Program Files\Anaconda3\lib\site-packages\openpyxl\cell\read_only.py", line 23, in _cast_number
    return long(value)

ValueError: invalid literal for int() with base 10: ' '

pandas代码:

import pandas as pd
df = pd.read_excel(r'C:/Temp/Data.xlsx', sheetname='Sheet1')

错误:

Traceback (most recent call last):

  File "<ipython-input-5-b86ec98a4e9e>", line 2, in <module>
    df = pd.read_excel(r'C:/Temp/Data.xlsx', sheetname='Sheet1')

  File "C:\Program Files\Anaconda3\lib\site-packages\pandas\io\excel.py", line 200, in read_excel
    io = ExcelFile(io, engine=engine)

  File "C:\Program Files\Anaconda3\lib\site-packages\pandas\io\excel.py", line 257, in __init__
    self.book = xlrd.open_workbook(io)

  File "C:\Program Files\Anaconda3\lib\site-packages\xlrd\__init__.py", line 422, in open_workbook
    ragged_rows=ragged_rows,

  File "C:\Program Files\Anaconda3\lib\site-packages\xlrd\xlsx.py", line 833, in open_workbook_2007_xml
    x12sheet.process_stream(zflo, heading)

  File "C:\Program Files\Anaconda3\lib\site-packages\xlrd\xlsx.py", line 548, in own_process_stream
    self_do_row(elem)

  File "C:\Program Files\Anaconda3\lib\site-packages\xlrd\xlsx.py", line 685, in do_row
    self.sheet.put_cell(rowx, colx, None, float(tvalue), xf_index)

ValueError: could not convert string to float: 

对于它的价值,这里是输入文件的示例片段: Input file example

我猜这些错误来自第一行的空白超出第一列 - 因为当我删除前两行时错误消失了。我不能跳过前两行,因为我想提取单元格A1中的值。我还想强制读取的值为字符串类型,稍后将转换为float并进行错误检查。谢谢!

===========

更新(美国东部时间8月9日上午10点):使用查理的建议,能够以只读模式打开excel文件;并且能够阅读大部分内容 - 但仍然在某处遇到错误。 新代码(抱歉它不是非常pythonic - 仍然是新手):

wb = openpyxl.load_workbook(r'C:/Temp/Data.xlsx', read_only=True)
ws = wb['Sheet1']
ws.max_row = ws.max_column = None

i=1
for row in ws.rows:
    for cell in row:
        if i<2000:
            i += 1
            try:
                print(i, cell.value)
            except:
                print("error")

错误:

Traceback (most recent call last):

  File "<ipython-input-65-2e8f3cf2294a>", line 2, in <module>
    for row in ws.rows:

  File "C:\Program Files\Anaconda3\lib\site-packages\openpyxl\worksheet\read_only.py", line 125, in get_squared_range
    yield tuple(self._get_row(element, min_col, max_col))

  File "C:\Program Files\Anaconda3\lib\site-packages\openpyxl\worksheet\read_only.py", line 165, in _get_row
    value, data_type, style_id)

  File "C:\Program Files\Anaconda3\lib\site-packages\openpyxl\cell\read_only.py", line 36, in __init__
    self.value = value

  File "C:\Program Files\Anaconda3\lib\site-packages\openpyxl\cell\read_only.py", line 132, in value
    value = _cast_number(value)

  File "C:\Program Files\Anaconda3\lib\site-packages\openpyxl\cell\read_only.py", line 23, in _cast_number
    return long(value)

ValueError: invalid literal for int() with base 10: ' '

======

Update2(上午10:35):当我读取文件没有 ws.max_row并且ws.max_column设置为None时,代码只读取一列,没有错误。单元格A66中的值是“Generated from:”。但当我读取文件 ws.max_row并且ws.max_column设置为None时,此特定单元格会导致问题。但是在此之前我可以阅读所有其他单元格,这对我来说很合适。谢谢,@查理。

2 个答案:

答案 0 :(得分:1)

听起来源文件可能已损坏,并且包含带有空字符串的单元格,这些字符串被输入为数字。您可以使用openpyxl的只读模式跳过第一行。

答案 1 :(得分:0)

如果您的程序在删除前两行后仍然有效,那么我们就跳过它们。尝试使用skiprows忽略前两行是空白或是标题。你可以使用熊猫的parse方法。

xls = pd.read_excel('C:/Temp/Data.xlsx')

df = xls.parse('Sheet1', skiprows=2) #assuming your data is on sheet1.