简体   繁体   中英

Save XLSX file to a specified location using OpenPyXL

I'm having an issue saving my file to a certain location on my Raspberry PI (Raspbian) computer. I'm wanting the XLSX file to be saved directly to my desktop rather than the folder holding the Python Script. When I do wb.save("FileName.xlsx") , It only saves it to the location where the Python Script is located.

Here's my code:

from openpyxl import Workbook
wb = Workbook()
ws1 = wb.active
ws1.title = "1st Hour"
wb.save('FileName.xlsx')

Okay for any user, you can write

from openpyxl import Workbook
import getpass
wb = Workbook()
ws1 = wb.active
ws1.title = "1st Hour"
wb.save('/home/'+getpass.getuser()+'/Desktop/FileName.xlsx')

On windows systems: first you must copy the path, for example this path:
C:\Users\obada yahya\Desktop\python

Now you must add another \ after each already existing \ to the path:

import openpyxl as xl
wb=xl.Workbook()
wb.save("C:\\Users\\obada yahya\\Desktop\\python\\obada12.xlsx")

Now it will work fine.

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