Home > Enterprise >  Create and import only last sheet data with found error "UserWarning: Calling close() on alread
Create and import only last sheet data with found error "UserWarning: Calling close() on alread

Time:07-27

When I run blow code I got error : UserWarning: Calling close() on already closed file. warn("Calling close() on already closed file.")

I cannot save my first three code and excel sheet also.

import openpyxl
from os import path
import pandas as pd

def load_workbook(wb_path):
    if path.exists(wb_path):
        return openpyxl.load_workbook(wb_path)
    return openpyxl.Workbook()

wb_path = './output/trail.xlsx'
wb = load_workbook(wb_path)

list = [530001, 530011, 530017, 530023]

for item in list:
    url_1 = f'https://www.screener.in/company/{item}/'

    df0 = pd.read_html(url_1)
        
    data1 = df0[1]
    
    with pd.ExcelWriter(wb_path) as writer:

        data1.to_excel(writer,sheet_name=str(item),startcol=0 ,startrow=1,index=False)
        
    writer.save()

CodePudding user response:

First things first: please don't call a list "list". list is a built-in data type in Python, which your code is overwriting. E.g.:

print(type(list))
<class 'type'>

list=[1,2,3]

print(type(list))
<class 'list'>

Your error is related to this info from the xslx-writer

  • Related