Home > OS >  How to create an excel file in a specific path
How to create an excel file in a specific path

Time:06-26

I know how to create a csv file in a specific path. The code is as follows.

csv_name=r'C:\Users\Technology\hello.csv'
with open(csv_name, 'wb') as csvfile:
    filewriter = csv.writer(csvfile, delimiter=',',
                            quotechar='|', quoting=csv.QUOTE_MINIMAL)

As you can see, I tried to change csv into excel file.

excel_name=r'C:\Users\Technology\hello.xlsx'
with open(excel_name, 'wb') as xlsxfile:
    filewriter = csv.writer(xlsxfile, delimiter=',',
                            quotechar='|', quoting=xlsx.QUOTE_MINIMAL)

I also tried in the other way as follows.

import xlsxwriter
workbook = xlsxwriter.Workbook(r'C:\Users\Technology\hello.xlsx')
workbook.close()

However, both of them don't work. Would you please help me find where the bug is.

CodePudding user response:

To create a blank Excel file in a specific path

import pandas as pd
excel_name =r'C:\Users\Technology\hello.xlsx'
Writer = pd.ExcelWriter(excel_name)
Writer.close()

To Export DataFrame to Excel

import pandas as pd
df = pd.DataFrame({'Col1':[1,2,3]})
excel_name =r'C:\Users\Technology\hello.xlsx'
Writer = pd.ExcelWriter(excel_name)
df.to_excel(Writer)
Writer.close()
  • Related