简体   繁体   中英

How to create an excel file in a specific path

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.

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM