4
respostas

Não consigo importar xlsx - outras soluções já foram tentadas

olá gente, já tentei diversas soluções e não consegui importar o xlsx. Nas próximas mensagens estarei enviando !conda list e o !conda info --envs

Mensagem de Erro:

---------------------------------------------------------------------------
XLRDError                                 Traceback (most recent call last)
<ipython-input-8-6d13ae696604> in <module>
----> 1 df_xlsx = pd.read_excel('dados/aluguel.xlsx')
      2 df_xlsx

~\.conda\envs\alura_pandas\lib\site-packages\pandas\io\excel\_base.py in read_excel(io, sheet_name, header, names, index_col, usecols, squeeze, dtype, engine, converters, true_values, false_values, skiprows, nrows, na_values, keep_default_na, verbose, parse_dates, date_parser, thousands, comment, skipfooter, convert_float, mangle_dupe_cols, **kwds)
    302 
    303     if not isinstance(io, ExcelFile):
--> 304         io = ExcelFile(io, engine=engine)
    305     elif engine and engine != io.engine:
    306         raise ValueError(

~\.conda\envs\alura_pandas\lib\site-packages\pandas\io\excel\_base.py in __init__(self, io, engine)
    822         self._io = stringify_path(io)
    823 
--> 824         self._reader = self._engines[engine](self._io)
    825 
    826     def __fspath__(self):

~\.conda\envs\alura_pandas\lib\site-packages\pandas\io\excel\_xlrd.py in __init__(self, filepath_or_buffer)
     19         err_msg = "Install xlrd >= 1.0.0 for Excel support"
     20         import_optional_dependency("xlrd", extra=err_msg)
---> 21         super().__init__(filepath_or_buffer)
     22 
     23     @property

~\.conda\envs\alura_pandas\lib\site-packages\pandas\io\excel\_base.py in __init__(self, filepath_or_buffer)
    351             self.book = self.load_workbook(filepath_or_buffer)
    352         elif isinstance(filepath_or_buffer, str):
--> 353             self.book = self.load_workbook(filepath_or_buffer)
    354         elif isinstance(filepath_or_buffer, bytes):
    355             self.book = self.load_workbook(BytesIO(filepath_or_buffer))

~\.conda\envs\alura_pandas\lib\site-packages\pandas\io\excel\_xlrd.py in load_workbook(self, filepath_or_buffer)
     34             return open_workbook(file_contents=data)
     35         else:
---> 36             return open_workbook(filepath_or_buffer)
     37 
     38     @property

~\.conda\envs\alura_pandas\lib\site-packages\xlrd\__init__.py in open_workbook(filename, logfile, verbosity, use_mmap, file_contents, encoding_override, formatting_info, on_demand, ragged_rows, ignore_workbook_corruption)
    168     # files that xlrd can parse don't start with the expected signature.
    169     if file_format and file_format != 'xls':
--> 170         raise XLRDError(FILE_FORMAT_DESCRIPTIONS[file_format]+'; not supported')
    171 
    172     bk = open_workbook_xls(

XLRDError: Excel xlsx file; not supported
4 respostas

!conda list:

# packages in environment at C:\Users\Henrique\.conda\envs\alura_pandas:
#
# Name                    Version                   Build  Channel
argon2-cffi               20.1.0           py38h2bbff1b_1  
async_generator           1.10               pyhd3eb1b0_0  
attrs                     20.3.0             pyhd3eb1b0_0  
backcall                  0.2.0              pyhd3eb1b0_0  
beautifulsoup4            4.9.3              pyhb0f4dca_0    anaconda
blas                      1.0                         mkl  
bleach                    3.3.0              pyhd3eb1b0_0  
ca-certificates           2021.4.13            haa95532_1  
certifi                   2020.12.5        py38haa95532_0  
cffi                      1.14.5           py38hcd4344a_0  
colorama                  0.4.4              pyhd3eb1b0_0  
decorator                 5.0.6              pyhd3eb1b0_0  
defusedxml                0.7.1              pyhd3eb1b0_0  
entrypoints               0.3                      py38_0  
html5lib                  1.1                        py_0    anaconda
importlib-metadata        3.10.0           py38haa95532_0  
importlib_metadata        3.10.0               hd3eb1b0_0  
intel-openmp              2020.2                      254  
ipykernel                 5.3.4            py38h5ca1d4c_0  
ipython                   7.22.0           py38hd4e2768_0  
ipython_genutils          0.2.0              pyhd3eb1b0_1  
jedi                      0.17.0                   py38_0  
jinja2                    2.11.3             pyhd3eb1b0_0  
jsonschema                3.2.0                      py_2  
jupyter_client            6.1.12             pyhd3eb1b0_0  
jupyter_core              4.7.1            py38haa95532_0  
jupyterlab_pygments       0.1.2                      py_0  
libiconv                  1.15             vc14h29686d3_5  [vc14]  anaconda
libsodium                 1.0.18               h62dcd97_0  
libxml2                   2.9.10               hb89e7f3_3    anaconda
libxslt                   1.1.34               he774522_0    anaconda
lxml                      4.6.1            py38h1350720_0    anaconda
m2w64-gcc-libgfortran     5.3.0                         6  
m2w64-gcc-libs            5.3.0                         7  
m2w64-gcc-libs-core       5.3.0                         7  
m2w64-gmp                 6.1.0                         2  
m2w64-libwinpthread-git   5.0.0.4634.697f757               2  
markupsafe                1.1.1            py38he774522_0  
mistune                   0.8.4           py38he774522_1000  
mkl                       2020.2                      256  
mkl-service               2.3.0            py38h196d8e1_0  
mkl_fft                   1.3.0            py38h46781fe_0  
mkl_random                1.1.1            py38h47e9c7a_0  
msys2-conda-epoch         20160418                      1  
nbclient                  0.5.3              pyhd3eb1b0_0  
nbconvert                 6.0.7                    py38_0  
nbformat                  5.1.3              pyhd3eb1b0_0  
nest-asyncio              1.5.1              pyhd3eb1b0_0  
notebook                  6.3.0            py38haa95532_0  
numpy                     1.19.2           py38hadc3359_0  
numpy-base                1.19.2           py38ha3acd2a_0  
openssl                   1.1.1k               h2bbff1b_0  
packaging                 20.9               pyhd3eb1b0_0  
pandas                    1.0.5            py38h47e9c7a_0  
pandoc                    2.12                 haa95532_0  
pandocfilters             1.4.3            py38haa95532_1  
parso                     0.8.2              pyhd3eb1b0_0  
pickleshare               0.7.5           pyhd3eb1b0_1003  
pip                       21.0.1           py38haa95532_0  

conda list:

prometheus_client         0.10.1             pyhd3eb1b0_0  
prompt-toolkit            3.0.17             pyh06a4308_0  
pycparser                 2.20                       py_2  
pygments                  2.8.1              pyhd3eb1b0_0  
pyparsing                 2.4.7              pyhd3eb1b0_0  
pyrsistent                0.17.3           py38he774522_0  
python                    3.8.8                hdbf39b2_5  
python-dateutil           2.8.1              pyhd3eb1b0_0  
pytz                      2021.1             pyhd3eb1b0_0  
pywin32                   227              py38he774522_1  
pywinpty                  0.5.7                    py38_0  
pyzmq                     20.0.0           py38hd77b12b_1  
send2trash                1.5.0              pyhd3eb1b0_1  
setuptools                52.0.0           py38haa95532_0  
six                       1.15.0           py38haa95532_0  
soupsieve                 2.0.1                      py_0    anaconda
sqlite                    3.35.4               h2bbff1b_0  
terminado                 0.9.4            py38haa95532_0  
testpath                  0.4.4              pyhd3eb1b0_0  
tornado                   6.1              py38h2bbff1b_0  
traitlets                 5.0.5              pyhd3eb1b0_0  
vc                        14.2                 h21ff451_1  
vs2015_runtime            14.27.29016          h5e58377_2  
wcwidth                   0.2.5                      py_0  
webencodings              0.5.1                    py38_1  
wheel                     0.36.2             pyhd3eb1b0_0  
wincertstore              0.2                      py38_0  
winpty                    0.4.3                         4  
xlrd                      2.0.1              pyhd3eb1b0_0  
zeromq                    4.3.3                ha925a31_3  
zipp                      3.4.1              pyhd3eb1b0_0  
zlib                      1.2.11           vc14h1cdd9ab_1  [vc14]  anaconda

!conda info --envs

# conda environments:
#
base                     C:\ProgramData\Anaconda3
alura_pandas          *  C:\Users\Henrique\.conda\envs\alura_pandas

Olá Henrique,

Isso acontece por uma mudança no xlrd (que o pandas utiliza por trás para ler arquivos do excel), exatamente na versão 2.0.1 ele não aceita mais nenhum tipo de arquivo além do .xls (o seu é .xlsx).

Você pode então tentar voltar para uma versão anterior dessa biblioteca como a 1.2.0 (tenho a 1.0.5 e funciona ok). Outra alternativa, a que eu recomendo, é utilizar a biblioteca openpyxl que pode substituir a xlrd, para instalar você pode executar o comando abaixo direto no jupyter notebook:

!pip install openpyxl

E no import você faz: pd.read_excel('dados/aluguel.xlsx', engine='openpyxl')

Espero que isso ajude, qualquer coisa é só falar!