Issue
I've extracted an excel file by running the code below.
from zipfile import ZipFile
with ZipFile('HISTDATA_COM_XLSX_EURUSD_M12018.zip', 'r') as zipObj:
zipObj.extractall()
After that, I wanted to open it. Due to being an xlsx file, I imported the openpyxl module and ran the code below.
from zipfile import ZipFile
import openpyxl
with ZipFile('HISTDATA_COM_XLSX_EURUSD_M12018.zip', 'r') as zipObj:
zipObj.extractall()
ref_workbook = openpyxl.load_workbook('DAT_XLSX_EURUSD_M1_2018.xlsx')
Then, I received this error: "Workbook contains no default style, apply openpyxl’s default in Python." So I also imported the warnings module and ran the code below:
import warnings
from zipfile import ZipFile
import openpyxl
warnings.filterwarnings("ignore")
with ZipFile('HISTDATA_COM_XLSX_EURUSD_M12018.zip', 'r') as zipObj:
zipObj.extractall()
ref_workbook = openpyxl.load_workbook('DAT_XLSX_EURUSD_M1_2018.xlsx')
But I received nothing in the output. Can anyone help? My operating system is Linux Ubuntu. The link of the file which I downloaded and extracted is: https://www.histdata.com/download-free-forex-historical-data/?/excel/1-minute-bar-quotes/eurusd/2018
P.S! I don't want to use the pandas module.
Solution
Your code does appear to be returning a reference to a workbook, but you're not reading any data from it. You need to specify a sheet and read data from cells.
import warnings
from zipfile import ZipFile
import openpyxl
warnings.filterwarnings("ignore")
with ZipFile('HISTDATA_COM_XLSX_EURUSD_M12018.zip', 'r') as zipObj:
zipObj.extractall()
ref_workbook = openpyxl.load_workbook('DAT_XLSX_EURUSD_M1_2018.xlsx')
sheet_2018 = ref_workbook['2018']
cell_A1 = sheet_2018['A1'].value
print(cell_A1)
cell_A1 = sheet_2018.cell(1, 1).value
print(cell_A1)
Output:
2018-01-01 17:00:00
2018-01-01 17:00:00
Answered By - Bill the Lizard Answer Checked By - Marilyn (WPSolving Volunteer)