PermissionError: [Errno 13] Permission denied: ‘name.pdf‘

Posted Data+Science+Insight

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PermissionError: [Errno 13] Permission denied: ‘name.pdf‘相关的知识,希望对你有一定的参考价值。

PermissionError: [Errno 13] Permission denied: 'name.pdf'

目录

PermissionError: [Errno 13] Permission denied: 'name.pdf'

问题:

解决:

完整错误:


问题:

因为我们在使用fpdf生成pdf的时候前面相同位置的同一个pdf正处在被打开的状态,所以无法创建。

解决:

我们可以关闭这个打开的同位置同名的pdf或者使用浏览器打开我们需要调试的pdf;

from fpdf import FPDF
from PIL import Image
import os

now = datetime.now() # current date and time
date_time = now.strftime("%Y-%m-%d")
date_time

# class PDF(FPDF):
#     # header
#     def header(self):
#         # Logo
#         #self.image('logo_pb.png', 10, 8, 33)
#         # Arial bold 15
#         #self.set_font('Arial', 'B', 30)
#         #self.add_font('STZHONGS', '', r"C:\\Windows\\Fonts\\STZHONGS.TTF", uni=True)
#         self.add_font('siyuan','',r"C:\\Users\\liguangdi\\AppData\\Local\\Microsoft\\Windows\\Fonts\\SourceHanSansSC-VF.ttf",True)
#         self.set_font('siyuan', '', 80)
#         # Move to the right
#         self.cell(200)
#         # Title
#         self.cell(300, 300, "结果报告", 1000, 1000, 'C')
#         # Line break
#         self.ln(20)
    
    # Page footer
#     def footer(self):
#         # Position at 1.5 cm from bottom
#         self.set_y(-15)
#         # Arial italic 8
#         self.set_font('Arial', 'I', 8)
#         # Page number
#         self.cell(0, 10, 'Page ' + str(self.page_no()) + '/{nb}', 0, 0, 'C')

def makePdf(pdfFileName, listPages):
    
    cover = Image.open(listPages[0])
    width, height = cover.size
    
    #pdf = FPDF(unit = "pt", format = [width, height])
    #pdf = PDF(unit = "mm",format = [width, height])
    pdf = FPDF(unit = "mm",format = [width, height])
    #pdf.alias_nb_pages()
    pdf.add_page()
    pdf.add_font('siyuan','',r"C:\\Users\\liguangdi\\AppData\\Local\\Microsoft\\Windows\\Fonts\\SourceHanSansSC-VF.ttf",True)
    pdf.set_font('siyuan', '', 100)
    #pdf.text(10, 10, 'test')
    pdf.text(400, 100, '结果报告')
    pdf.set_font('siyuan', '', 30)
    pdf.text(400, 300, '报告日期:' + date_time)
    pdf.text(400, 400, '作者:小李、小屁卡、season、mandy、shiwu、lgd')
    #
    pdf.text(400, 500, '麦田(苏州)医学科技有限公司')
    #pdf = FPDF(unit = "mm",format = [width, height])
    
    ratio = height/width
    #print(ratio)
    for page in listPages:
        pdf.add_page()
        pdf.image(page, 0, 0,w=width,h=height)
    
    pdf.add_page()
    pdf.add_font('siyuan','',r"C:\\Users\\liguangdi\\AppData\\Local\\Microsoft\\Windows\\Fonts\\SourceHanSansSC-VF.ttf",True)
    pdf.set_font('siyuan', '', 14)
    pdf.set_top_margin(2.54*100)
    pdf.set_left_margin(2.18*100)
    pdf.ln(2)

    pdf.set_font('siyuan', '', 30)
    epw = pdf.w - 2*pdf.l_margin
    col_width = epw/8
    pdf.cell(epw, 5, u'指标报告', align='C')
    pdf.set_font('siyuan', '', 20)
    pdf.ln(2)
    pdf.cell(epw*2, 25, u'总体参数:', align='L')

    pdf.ln(1.0)

#     item = ['AUC','best_alpha','intercept','feature_selection','train_size','test_size','model']
#     value = [str(auc),str(best_alpha),str(intercept),'lasso',str(0.8),str(0.2),pipeline_optimizer.fitted_pipeline_.steps[1][0]]
    item = ['样本数','初始维度','model','Accuracy(test)','Accuracy(train)','AUC(test)','AUC(train)','best_alpha','intercept','feature_selection','train_size','test_size','test_0','test_1','train_0','train_1']
    value = [str(df_origin.shape[0]),str(df_origin.shape[1]),pipeline_optimizer.fitted_pipeline_.steps[1][0],str(rep['accuracy']),str(rep_train['accuracy']),str(test_auc),str(train_auc),str(best_alpha),str(intercept),'lasso',str(0.8),str(0.2),str(rep['0']),str(rep['1']),str(rep_train['0']),str(rep_train['1'])]
 
    pdf.set_font('siyuan', '', 12)
    for i in range(1,17):
        if i%2 == 1:
            pdf.cell(col_width/4,0,' ')
        pdf.cell(col_width,40,item[i-1]+':')
        pdf.cell(col_width*4.5,40,value[i-1])
        if i % 2 == 0:
            pdf.ln(0.8*10)
    
    pdf.set_font('siyuan', '', 15)
    pdf.ln(2)
    pdf.cell(epw*2, 80, u'radiomics formula:', align='L')
    pdf.ln(1.0)
    #pdf.cell(col_width)
    # Centered text in a framed 20*10 mm cell and line break
    #pdf.cell(100, 200, formula_res, 1, 1, 'L')
    #pdf.cell(col_width,100,formula_res,ln = 2,align='L')
    step = len(formula_res)//250+1
    for index in range(0,step):
        y = 110+(index+2)*10
        pdf.text(col_width*4, y, formula_res[index*250:(index+1)*250])
        #pdf.text(10, 10, pdftext)
    
    pdf.set_font('siyuan', '', 15)
    pdf.ln(2)
    pdf.cell(epw*2, 450, 'Accuracy CV:  '+str(accs)+' mean='+str(mean_acc)+ ' std='+str(std_acc), align='L')
    pdf.ln(1.0)
    pdf.set_font('siyuan', '', 15)
    pdf.ln(2)
    pdf.cell(epw*2, 470, 'AUC CV :  '+str(aucs)+' mean='+str(mean_auc)+ ' std='+str(std_auc), align='L')
    
    pdf.add_page()
    pdf.add_font('siyuan','',r"C:\\Users\\liguangdi\\AppData\\Local\\Microsoft\\Windows\\Fonts\\SourceHanSansSC-VF.ttf",True)
    pdf.set_font('siyuan', '', 20)
    pdf.set_top_margin(2.54*100)
    pdf.set_left_margin(2.18*100)
    pdf.set_font('siyuan', '', 20)
    pdf.ln(0.5)
    pdf.cell(epw, -480, u'系数表:', align='L')
    pdf.ln(1.0)
    pdf.set_font('siyuan', '', 15)
    pdf.cell(col_width/4,0,'')
    pdf.cell(col_width*3,-450,'特征')
    pdf.cell(col_width*2,-450,'系数')
    pdf.ln(1.0)
    item = features.columns[regr_cv.coef_[0] != 0].tolist()
    value = regr_cv.coef_[0][regr_cv.coef_[0] != 0].tolist()
    for i in range(0,len(item)):
        pdf.cell(col_width / 4, 0, '')
        pdf.cell(col_width*3,-430,item[i])
        pdf.cell(col_width*3,-430,str(value[i]))
        pdf.ln(8)
    pdf.ln(1.0)
    pdf.cell(col_width*1.5, 0, '')
    
    
#     pdf.add_page()
#     pdf.add_font('siyuan','',r"C:\\Users\\liguangdi\\AppData\\Local\\Microsoft\\Windows\\Fonts\\SourceHanSansSC-VF.ttf",True)
#     pdf.set_font('siyuan', '', 14)
#     pdf.text(0, 5, '模型指标总结:')
#     pdf.cell(0, 6, 'auc',1,ln=0,align="L")
#     pdf.cell(0, 6, str(auc),1,ln=1,align="R")
#     pdf.cell(0, 6, "best_alpha",1,ln=0, align="L")
#     pdf.cell(0, 6, "intercept",ln=1, align="R")
    
    #pdf.text(0, 0, str(pdftext))
#     for index,line in enumerate(lines):
#         y = (index+2)*5
#         pdf.text(0, y, line)
#         #pdf.text(10, 10, pdftext)
    pdf.output(pdfFileName, "F")
    
# makePdf("result.pdf", [imgFileName for imgFileName in os.listdir('.') \\
# if imgFileName.endswith("png")])

makePdf(str(timestamp) +"graph.pdf", [imgFileName for imgFileName in final_filelist])

完整错误:

---------------------------------------------------------------------------
PermissionError                           Traceback (most recent call last)
<ipython-input-501-71b1ccb92bfa> in <module>
    157 # if imgFileName.endswith("png")])
    158 
--> 159 makePdf(str(timestamp) +"graph.pdf", [imgFileName for imgFileName in final_filelist])

<ipython-input-501-71b1ccb92bfa> in makePdf(pdfFileName, listPages)
    152 #         pdf.text(0, y, line)
    153 #         #pdf.text(10, 10, pdftext)
--> 154     pdf.output(pdfFileName, "F")
    155 
    156 # makePdf("result.pdf", [imgFileName for imgFileName in os.listdir('.') \\

D:\\anaconda\\lib\\site-packages\\fpdf\\fpdf.py in output(self, name, dest)
   1077         elif dest=='F':
   1078             #Save to local file
-> 1079             f=open(name,'wb')
   1080             if(not f):
   1081                 self.error('Unable to create output file: '+name)

PermissionError: [Errno 13] Permission denied: '1631588177000graph.pdf'

以上是关于PermissionError: [Errno 13] Permission denied: ‘name.pdf‘的主要内容,如果未能解决你的问题,请参考以下文章

PermissionError Errno 13 in python

PermissionError: [Errno 13] 权限被拒绝

mac 在终端上运行脚本,提示PermissionError: [Errno 1] Operation not permitted

mac 在终端上运行脚本,提示PermissionError: [Errno 1] Operation not permitted

PermissionError: [Errno 1] for os.rename as quick action

PermissionError: [Errno 13] Permission denied: ‘name.pdf‘