1

good day,

I am attempting to open multiple excel files (xls) files and put them in one data frame. I am using .glob() to access the files here:

all_files = glob.glob('D:\Anaconda Hub\ARK analysis\Ark analysis\data\year2021\\february\\**.xls')

The sample output is a list so:

['D:\\Anaconda Hub\\ARK analysis\\Ark analysis\\data\\year2021\\february\\ARK_Trade_02012021_0619PM_EST_601875e069e08.xls',
 'D:\\Anaconda Hub\\ARK analysis\\Ark analysis\\data\\year2021\\february\\ARK_Trade_02022021_0645PM_EST_6019df308ae5e.xls',
 'D:\\Anaconda Hub\\ARK analysis\\Ark analysis\\data\\year2021\\february\\ARK_Trade_02032021_0829PM_EST_601b2da2185c6.xls',
 'D:\\Anaconda Hub\\ARK analysis\\Ark analysis\\data\\year2021\\february\\ARK_Trade_02042021_0637PM_EST_601c72b88257f.xls',
 'D:\\Anaconda Hub\\ARK analysis\\Ark analysis\\data\\year2021\\february\\ARK_Trade_02052021_0646PM_EST_601dd4dc308c5.xls',
 'D:\\Anaconda Hub\\ARK analysis\\Ark analysis\\data\\year2021\\february\\ARK_Trade_02082021_0629PM_EST_6021c739595b0.xls'..]

I am using the olefile method. Here is my code:

import os
import glob
import olefile as ol
import pandas as pd

 # using olefile to iterate to extract each excel file to be readible 
with open(all_files,'r') as file:
    if file.endswith('.xls'):
        ole = ol.OleFileIO(file)
        if ole.exists('Workbook'):
            d = ole.openstream('Workbook')
            df = pd.read_excel(d, engine='xlrd', header=3, skiprows=3)
            print(df.head())

However, I get this error:

TypeError: expected str, bytes or os.PathLike object, not list

I am not understanding why I am obtaining this error. I am iterating over the list to select a string and pass it through the rest of the steps... Help would be appreciated to do this correctly and get the excel files to output in a single data frame. Thanks in advance

Deepak M
  • 1,124
  • 2
  • 18
  • 28

1 Answers1

0

I believe you are working with a legacy format/version of Microsoft Excel?

The error message TypeError: expected str, bytes or os.PathLike object, not list is pretty informative in this case. Your code has the line: with open(all_files,'r') as file:, where you have passed the entire list to open().

Try the following code:

import os
import glob
import olefile
import pandas as pd

all_files = glob.glob('excelfiles/*.xls')

for file in all_files:
    with olefile.OleFileIO(file) as ole:     # Since olefile v0.46
        if ole.exists('Workbook'):
            d = ole.openstream('Workbook')
            df = pd.read_excel(d, engine='xlrd', header=3, skiprows=3)
            print(df.head())

Output I have from the files shared:

   ARKG  2021-02-01  Sell  ... PACIFIC BIOSCIENCES OF CALIFORNIA INC  210508  0.0645
0  ARKK  2021-02-01   Buy  ...                 FATE THERAPEUTICS INC  154509  0.0608
1  ARKK  2021-02-01   Buy  ...                            PACCAR INC  263029  0.1024
2  ARKK  2021-02-01   Buy  ...                          TERADYNE INC  295371  0.1465
3  ARKK  2021-02-01   Buy  ...                 BEAM THERAPEUTICS INC   58218  0.0241
4  ARKK  2021-02-01  Sell  ...         REGENERON PHARMACEUTICALS INC    5130  0.0111

[5 rows x 8 columns]
   ARKG  2021-02-03  Sell  ...  TWIST BIOSCIENCE CORP   97415  0.1615
0  ARKK  2021-02-03   Buy  ...  SPOTIFY TECHNOLOGY SA  385932  0.4980
1  ARKK  2021-02-03   Buy  ...             PACCAR INC  318474  0.1231
2  ARKK  2021-02-03   Buy  ...  FATE THERAPEUTICS INC   98059  0.0394
3  ARKK  2021-02-03   Buy  ...           TERADYNE INC  104809  0.0524
4  ARKK  2021-02-03  Sell  ...               ROKU INC   53551  0.0924

[5 rows x 8 columns]
anurag
  • 1,715
  • 1
  • 8
  • 28