Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PANDAS: Execute to_excel successfully,but no output file

When I use to_excel to generate excel file, no error occurred , but no output file available. can't find anything,any file in my supposed location... And I don't know why..

import pandas as pd
import os
import xlrd
import openpyxl
pd.set_option('display.width',None)
DIR = 'E:\Process'
datapath =os.path.join(DIR, 'Data.xlsx')
formatpath = os.path.join(DIR, 'Format.xlsx')
df = pd.read_excel(datapath)
df1=pd.read_excel(formatpath)
for i in range(0, len(df)):
    target = (df.iloc[i,17])
    df2 = df1
    df2.iat[3,3] = target
    print(df2)
    filename = df.iloc[i,2]
    filename = str(filename) + ".xlsx"
    sourcepath = os.path.join(DIR, filename)
    writer = pd.ExcelWriter(sourcepath)
    df2.to_excel(writer)
    print(sourcepath)
like image 235
Codefmeister Avatar asked Oct 16 '25 20:10

Codefmeister


1 Answers

Expanding on the comment:

Use writer.save() after calling to_excel.

Alternatively you can use the with statement as suggested in the docs: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.ExcelWriter.html

like image 104
Dan Avatar answered Oct 18 '25 10:10

Dan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!