How to Get Stock Data and Export It to Excel Using Python
Pull a ticker’s daily history with the yfinance package and write it to Excel with pandas: open, high, low, close and volume over whatever date range you set. One short script, one ticker symbol to change, and the workbook is ready for whatever analysis follows.
Update (November 2026): The video uses the pandas-datareader Yahoo source, which no longer works since Yahoo changed its API, so the code below uses yfinance instead (tested with yfinance 1.8 and pandas 3.1).
Install the packages
The script needs three packages: yfinance downloads the prices from Yahoo Finance, pandas holds them in a DataFrame and openpyxl writes the .xlsx file. Open the command prompt (on Windows, press Windows + R, type cmd and press Enter) and install them:
pip install yfinance pandas openpyxl
yfinance is an unofficial wrapper around Yahoo’s public data, not a Yahoo product. It is fine for learning and personal projects, but Yahoo can change its endpoints at any time, so update it with pip install --upgrade yfinance if downloads start failing.
Download one stock
Create a file named stock_retrieve.py with the date range, the ticker symbol and three lines that download, clean and export the data:
import yfinance as yf # pip install yfinance
import pandas as pd # pip install pandas openpyxl
start = "2020-01-01"
end = "2020-07-31"
stock = "GOOG"
df = yf.download(stock, start=start, end=end, multi_level_index=False)
df.index = df.index.strftime("%Y-%m-%d")
df.to_excel(f"stock_data_{stock}.xlsx")
- start and end set the date range. The end date is exclusive, so this range stops at July 30, 2020.
- stock is the ticker symbol as Yahoo Finance writes it, here GOOG for Alphabet. Look up the exact symbol on finance.yahoo.com, especially for stocks outside the US, which carry a suffix such as .L or .DE.
- multi_level_index=False gives one header row. Without it, yfinance returns two header levels (price and ticker) even for a single stock.
- strftime writes the dates as YYYY-MM-DD text. Without that line, Excel gets real dates with a 00:00:00 time part.
The script saves stock_data_GOOG.xlsx with the columns Date, Close, High, Low, Open and Volume. yfinance adjusts the prices for splits and dividends by default, so GOOG shows about 68 in early January 2020 instead of the roughly 1,360 in the video, because of the 20-for-1 split in 2022. Pass auto_adjust=False to get the raw prices plus a separate Adj Close column.
Run the script
Run the script from the folder it is saved in. In the command prompt, use the cd command followed by the path to that folder, for example:
cd Desktop/stock_retrieve
Then run the script:
python stock_retrieve.py
Several stocks at once
For several stocks, pass the ticker symbols as one string separated by spaces. yfinance downloads them in one call and pandas writes them to one sheet:
import yfinance as yf # pip install yfinance
import pandas as pd # pip install pandas
start = "2020-01-01"
end= "2020-07-31"
tickers = 'GOOG TSLA MMM'
df = yf.download(tickers, start=start, end=end)
df.index = df.index.strftime('%Y-%m-%d')
df.to_excel('stocks.xlsx')
With several tickers, the columns come back with two header rows: the price type (Close, High, Low, Open, Volume) on top and the ticker below it, so each ticker gets its own column under each price. The tickers are sorted alphabetically within each block.

If you later need several DataFrames in one workbook, for example one sheet per ticker, see how to export pandas DataFrames to a new or existing Excel workbook.
Troubleshooting
Most problems come from an empty download or from code written for the old pandas-datareader version.
- An empty DataFrame or an empty sheet: the ticker is misspelled, the range has no trading days, or Yahoo rate-limited the request. Check the symbol on finance.yahoo.com, widen the date range and try again a little later.
- NotImplementedError from pandas_datareader: the Yahoo source is gone. Use the yfinance code above.
- KeyError: ‘Adj Close’: yfinance adjusts prices by default now. Pass
auto_adjust=Falseor use the Close column. - PermissionError when saving: the workbook is still open in Excel. Close it and run the script again.
Going further
Once the prices are in a DataFrame, charts and trackers are a few lines away.
My post on creating a candlestick chart in Python with Plotly uses the same download. If you prefer to stay in VBA, I also show how to get stock data in Excel with VBA. For a bigger project built on the same data, try my dividend portfolio tracker in Excel with Python.
