简体   繁体   English

使用 OpenPyXL 将 XLSX 文件保存到指定位置

[英]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.我在将文件保存到 Raspberry PI (Raspbian) 计算机上的某个位置时遇到问题。 I'm wanting the XLSX file to be saved directly to my desktop rather than the folder holding the Python Script.我希望将 XLSX 文件直接保存到我的桌面,而不是保存 Python 脚本的文件夹。 When I do wb.save("FileName.xlsx") , It only saves it to the location where the Python Script is located.当我执行wb.save("FileName.xlsx")时,它只会将其保存到 Python 脚本所在的位置。

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:在 Windows 系统上:首先您必须复制路径,例如此路径:
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.现在它会正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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