pythonpandasopenpyxlexcel-writer-xlsx

What is the best way to write to a sheet without overwriting an excel file?


I have an Excel file named MasterFile. Within MasterFile I have multiple sheets with formulas. I would like to use the code below to update one sheet in MasterFile without overwriting any of my data or formulas.

Here is my code so far:

from bs4 import BeautifulSoup
import requests
import pandas as pd
from openpyxl import load_workbook

url = 'http://www.baseballpress.com/lineups'

soup = BeautifulSoup(requests.get(url).text, 'html.parser')

players = [i.text for i in soup.find_all('a', {'class': 'player-link'})]


my_dict = (players)

df = pd.DataFrame(pd.Series(my_dict))

writer = pd.ExcelWriter('my2nd_webscrape.xlsx')
df.to_excel(writer,'Sheet1')
writer.save()

I have found some info regarding this subject in How to write to an existing excel file without breaking formulas with openpyxl?, but I am not sure how to adjust my code.


Solution

  • Try this:

    from bs4 import BeautifulSoup
    import requests
    import pandas as pd
    from openpyxl import load_workbook
    
    book = load_workbook('my2nd_webscrape.xlsx')
    writer = pd.ExcelWriter('my2nd_webscrape.xlsx')
    writer.book = book
    
    url = 'http://www.baseballpress.com/lineups'
    soup = BeautifulSoup(requests.get(url).text, 'html.parser')
    players = [i.text for i in soup.find_all('a', {'class': 'player-link'})]
    my_dict = (players)
    
    df = pd.DataFrame(pd.Series(my_dict))
    df.to_excel(writer,'Sheet1')
    writer.save()
    writer.close()