简体   繁体   English

PermissionError:[Errno 13] openpyxl python 中的权限被拒绝

[英]PermissionError: [Errno 13] Permission denied in openpyxl python

I am creating Invoice generator using Tkinter python but I am getting permission error All the respective files were closed during program execution.我正在使用 Tkinter python 创建发票生成器,但我收到权限错误所有相应的文件在程序执行期间都已关闭。 In Tkinter window, once I click add button it shows the error which is mentioned below在 Tkinter window 中,一旦我单击添加按钮,它就会显示下面提到的错误

My program我的程序

from tkinter import *
from openpyxl import load_workbook
from datetime import date

# from win32com import client
wb = load_workbook('E://invoice.xlsx')
sheet = wb['Sheet1']


#Date 
sheet.cell(row=7,column=6).value = date.today()
#BILL NUMBER 
temp = sheet.cell(row=8,column=6).value
temp = int(temp[1:])
new_no = temp+1
sheet.cell(row=8,column=6).value = 'B0'+str(new_no)

#Making Sheet values blank
sheet.cell(row=8,column=1).value = ""
for i in range(17,27):
    sheet.cell(row=i,column=1).value = None
    sheet.cell(row=i,column=3).value = None
    sheet.cell(row=i,column=4).value = None
co=16     
def get1():
    global co
    co+=1
    print("get")
    global ent1
    global ent2
    global ent3
    global ent4
    client_name = ent1.get()
    item = ent2.get()
    price = ent3.get()
    quantity = ent4.get()
    sheet.cell(row=8,column=1).value = client_name
    sheet.cell(row=co,column=1).value = item
    sheet.cell(row=co,column=3).value = price
    sheet.cell(row=co,column=4).value = quantity
    print(client_name,item,price,quantity)
    wb.save('E://invoice.xlsx')

def pdf():
    global wb
    wb.close()
    from win32com import client
    from openpyxl import load_workbook
    wb = load_workbook('E://invoice.xlsx')
    sheet = wb['Sheet1']
    #sheet name
    bill_no = sheet.cell(row=8,column=6).value
    cus_name = sheet.cell(row=8,column=1).value

    input_file = r'E:invoice.xlsx'
    output_file = r'E:exel//'+(bill_no+'_'+cus_name)+'.pdf'
    app = client.DispatchEx("Excel.Application")
    app.Interactive = False
    app.Visible = False
    Workbook = app.Workbooks.Open(input_file)
    try:
        Workbook.ActiveSheet.ExportAsFixedFormat(0, output_file)
    except Exception as e:
        print("Failed to convert in PDF format.Please confirm environment meets all the requirements  and try again")
        print(str(e))
    finally:
        Workbook.Close()
#     app.Exit()

    
    
window = Tk(screenName = "vcs",baseName = "ver 1.0")
window.geometry("500x500")
window.title("Vaibhav Computer Services")
widget = Label(window, text = "Vaibhav Computer Services",fg='blue',font=("bold",16))
widget.pack()


lab1 = Label(window, text = "Client Name :",fg='blue',font=("bold",16)).place(x=90,y=60)
ent1 = Entry(window)
ent1.place(x=230,y=65)

lab2 = Label(window, text = "Item  :",fg='blue',font=("bold",16)).place(x=90,y=100)
ent2 = Entry(window)
ent2.place(x=230,y=105)
print(ent2)

lab3 = Label(window, text = "Price  :",fg='blue',font=("bold",16)).place(x=90,y=140)
ent3 = Entry(window)
ent3.place(x=230,y=145)
# ent3.bind('<KeyPress>',keybind1)
print(ent3)

lab4 = Label(window, text = "Quantity  :",fg='blue',font=("bold",16)).place(x=90,y=180)
ent4 = Entry(window)
ent4.place(x=230,y=185)
print(ent4)

#Button
button1 = Button(window,text="Add",font=("bold"),command=get1).place(x=250,y=230)

button2 = Button(window,text="Generate PDF",font=("bold"),command=pdf).place(x=200,y=290)
window.mainloop()
wb.save('E://invoice.xlsx')

All the respective files was closed during program execution在程序执行期间,所有相应的文件都已关闭

Error错误

Exception in Tkinter callback
Traceback (most recent call last):
  File "c:\users\pranu\appdata\local\programs\python\python38-32\lib\tkinter\__init__.py", line 1883, in __call__
    return self.func(*args)
  File "<ipython-input-22-6691247dc7ef>", line 44, in get1
    wb.save('E://invoice.xlsx')
  File "c:\users\pranu\appdata\local\programs\python\python38-32\lib\site-packages\openpyxl\workbook\workbook.py", line 392, in save
    save_workbook(self, filename)
  File "c:\users\pranu\appdata\local\programs\python\python38-32\lib\site-packages\openpyxl\writer\excel.py", line 291, in save_workbook
    archive = ZipFile(filename, 'w', ZIP_DEFLATED, allowZip64=True)
  File "c:\users\pranu\appdata\local\programs\python\python38-32\lib\zipfile.py", line 1251, in __init__
    self.fp = io.open(file, filemode)
PermissionError: [Errno 13] Permission denied: 'E://invoice.xlsx'

In my computer this error will appear when the file is open and we use wb.save("E://invoice.xlsx") .在我的计算机中,当文件打开并且我们使用wb.save("E://invoice.xlsx")时,会出现此错误。 also see this code.另请参阅此代码。

from os import startfile
from openpyxl import load_workbook

# now file will start
startfile("Test.xlsx")

workbook = load_workbook("Test.xlsx")
sheet = workbook.active

# sheet.cell(row = 1, column = 1, value = "Hello")
sheet["A1"] = "Hello"

workbook.save("Test.xlsx")

and the output is: output 是:

Traceback (most recent call last):
  File "C:/Test.py", line 10, in <module>
    workbook.save("a.xlsx")
  File "C:\Python\Project\venv\lib\site-packages\openpyxl\workbook\workbook.py", line 409, in save
    save_workbook(self, filename)
  File "C:\Python\Project\venv\lib\site-packages\openpyxl\writer\excel.py", line 292, in save_workbook
    archive = ZipFile(filename, 'w', ZIP_DEFLATED, allowZip64=True)
  File "C:\Python\Python 3.8\lib\zipfile.py", line 1251, in __init__
    self.fp = io.open(file, filemode)
PermissionError: [Errno 13] Permission denied: 'Test.xlsx'

but if we use但是如果我们使用

from os import startfile
from openpyxl import load_workbook    

workbook = load_workbook("Test.xlsx")
sheet = workbook.active

# sheet.cell(row = 1, column = 1, value = "Hello")
sheet["A1"] = "Hello"

workbook.save("Test.xlsx")

# now file will start
startfile("Test.xlsx")

which the file start in the end we have no error.其中文件开始到底我们没有错误。 In the first one file was running and then we see that error but in the second the file was not running and we used startfile("Test.xlsx") after the workbook.save("Test.xlsx") .在第一个文件正在运行,然后我们看到该错误,但在第二个文件没有运行,我们在startfile("Test.xlsx") workbook.save("Test.xlsx")

also send "invoice.xlsx" to "benyamin.zavieh.ba@gmail.com" i want to test some code that if it work i will send it to you:)还将“invoice.xlsx”发送到“benyamin.zavieh.ba@gmail.com”我想测试一些代码,如果它工作我会把它发送给你:)

(I have some ideas about your code i'm glad it will work) (我对您的代码有一些想法,我很高兴它会起作用)

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

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