AttributeError:'NoneType'对象没有属性'读取'

AttributeError: #39;NoneType#39; object has no attribute #39;read#39;(AttributeError:#39;NoneType#39;对象没有属性#39;读取#39;)
本文介绍了AttributeError:'NoneType'对象没有属性'读取'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Openpyxl保存Excel文件,但在执行代码后出现此错误。

我的代码:

def assemble_files(self):
    global names

    wb = load_workbook(names[0])
    sheet = wb.get_active_sheet()                           
    exportFile = asksaveasfilename( defaultextension=".xlsx", filetypes=(("Excel file", "*.xlsx"),("All Files", "*.*") ))
    wb.save(exportFile)
    messagebox.showinfo("Information", "Files successfully assembled")`

错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:Usersmohamed.louatiAppDataLocalProgramsPythonPython36lib	kinter\__init__.py", line 1699, in __call__
    return self.func(*args)
  File "C:Usersmohamed.louatiDesktopainesID_Extractor.py", line 98, in assemble_files
    wb.save(exportFile)
  File "C:Usersmohamed.louatiAppDataLocalProgramsPythonPython36libsite-packagesopenpyxlwriterexcel.py", line 180, in _write_comment
    vml = fromstring(self.workbook.vba_archive.read(ws.legacy_drawing))
AttributeError: 'NoneType' object has no attribute 'read'

任何人都可以帮助解决这个问题

推荐答案

完整代码如下:

 # -*- coding: utf-8 -*-

import tkinter as tk
import tkinter.ttk as ttk
from tkinter.filedialog import askopenfilename,askopenfilenames,asksaveasfilename
from tkinter import messagebox
import xlrd
from openpyxl import *
from openpyxl.styles.borders import Border, Side
name = None
names = None

class Application(tk.Frame):

    def __init__(self, container):
        super().__init__(container)
        self.container = container
        self.document = None
        self.name_sheet = None
        self.type_sheet = "Type1"
        self.code = ""
        self.widgets()

    def list_paterns(self,document):
        feuilles = []
        res = len(document.sheet_names())
        names = document.sheet_names()
        for i in range(res):
            feuilles.append(document.sheet_by_index(i))
        return feuilles,names

    def widgets(self):

        menu = tk.Menu(self.container)
        root.config(menu=menu)        
        filemenu = tk.Menu(menu, tearoff=0)
        menu.add_cascade(label="File", menu=filemenu)
        filemenu.add_command(label="Generate", command = self.generate)
        filemenu.add_command(label="Assemble", command = self.assemble)      
        filemenu.add_separator()
        filemenu.add_command(label="Exit", command = self.do_exit)

    def generate(self):
        liste = self.container.winfo_children()
        for item in liste :
            if item.winfo_children() :
                liste.extend(item.winfo_children())
        for item in liste:
            item.place_forget()

        load = tk.Button(self.container,text="Load file",command=self.load_template, width = 10)
        load.place(x=20,y=20)

        generate_type = tk.Button(self.container,text="Generate", command= lambda: self.generate_code(self.name_sheet), width = 10)
        generate_type.place(x=250,y=20)

    def assemble(self):

        liste = self.container.winfo_children()
        for item in liste :
            if item.winfo_children() :
                liste.extend(item.winfo_children())
        for item in liste:
            item.place_forget()

        load = tk.Button(self.container,text="Select files",command=self.load_files, width = 10)
        load.place(x=20,y=20)

        assemble = tk.Button(self.container,text="Assemble", command= self.assemble_files, width = 10)
        assemble.place(x=250,y=20)

    def do_exit(self):
        root.destroy()


    def load_files(self):
        """  load files  """
        global names
        names = askopenfilenames(parent=root,title='Choose a file')


    def assemble_files(self):
        """  assemble files  """
        global names

        wb = load_workbook(names[0])
        sheet = wb.get_active_sheet() 
        for file in names:
            wb1 = load_workbook(file) 
            current = wb1.worksheets[0]
            for lin in range(1,current.max_row+1):
                for col in range(1,current.max_column+1):
                    if (hasattr(current.cell(row=lin, column=col), "value")):

                        if current.cell(row=lin, column=col).value != sheet.cell(row=lin, column=col).value and current.cell(row=lin, column=col).value != None:
                            sheet.cell(row=lin, column=col).value = current.cell(row=lin, column=col).value
        exportFile = asksaveasfilename( defaultextension=".xlsx", filetypes=(("Excel file", "*.xlsx"),("All Files", "*.*") ))
        print(sheet.cell(row=27, column=5).value)
        wb.save(exportFile)
        messagebox.showinfo("Information", "Files successfully assembled")


    def load_template(self):
        global name
        def name_sheet(event):
            self.name_sheet = event.widget.get()

        def type_sheet(event1):
            self.type_sheet = event1.widget.get()

        name = askopenfilename() #select file

        self.document = xlrd.open_workbook(name) #open template
        feuilles,names = self.list_paterns(self.document)

        self.name_sheet = names[0]

        cb1 = ttk.Combobox(self.container,width=30)
        cb1['values'] = names
        cb1.set(names[0])
        cb1.place(x=20,y=100)
        cb1.bind('<<ComboboxSelected>>', name_sheet)


        cb = ttk.Combobox(self.container, values=("Type1", "Type2", "Type3"),width=10)
        cb.set("Type1")
        cb.place(x=230,y=100)
        cb.bind('<<ComboboxSelected>>', type_sheet)


    def generate_code(self,name1):

        feuille=self.document.sheet_by_name(name1)
        thin_border = Border(left=Side(style='thin'), 
                     right=Side(style='thin'), 
                     top=Side(style='thin'), 
                     bottom=Side(style='thin'))
        i=0
        cell = " "
        wb=load_workbook(name)
        for i in wb.get_sheet_names():
            wb.remove_sheet(wb.get_sheet_by_name(i))
        wb.create_sheet(self.type_sheet)
        sheet = wb[self.type_sheet] 
        for i in range(1,feuille.nrows):
            cell = feuille.cell_value(i,0)
            S="A"+str(i+1)
            if 'MY20_L663_SysRS_FCT-Ass2_' in cell:
                y = cell.replace('MY20_L663_SysRS_FCT-Ass2_',' ')
                sheet[S].value = y
                sheet[S].border = thin_border
            if 'MY20_L663_SysRS_FCT-Ass1_' in cell:
                y = cell.replace('MY20_L663_SysRS_FCT-Ass1_',' ')
                sheet[S].value = y
                sheet[S].border = thin_border
        if self.type_sheet == "Type1":
            sheet["A1"].value = "ID"
            sheet["A1"].border = thin_border
            sheet["B1"].value = "Responsable"
            sheet["B1"].border = thin_border
            sheet["C1"].value = "Review"
            sheet["C1"].border = thin_border
            sheet["D1"].value = "Findings"
            sheet["D1"].border = thin_border
            sheet["E1"].value = "OK"
            sheet["E1"].border = thin_border
            sheet["F1"].value = "Comment (Optionnal)"
            sheet["F1"].border = thin_border

        if self.type_sheet == "Type2":
            sheet["A1"].value = "ID"
            sheet["A1"].border = thin_border
            sheet["B1"].value = "Responsable"
            sheet["B1"].border = thin_border
            sheet["C1"].value = "Input Status OK"
            sheet["C1"].border = thin_border
            sheet["D1"].value = "Number of already existing Testcases"
            sheet["D1"].border = thin_border
            sheet["E1"].value = "Tests created"
            sheet["E1"].border = thin_border
            sheet["F1"].value = "Number of Tests created"
            sheet["F1"].border = thin_border
            sheet["G1"].value = "Requirement covered"
            sheet["G1"].border = thin_border
            sheet["H1"].value = "Output Status OK"
            sheet["H1"].border = thin_border
            sheet["I1"].value = "Comment (Optionnal)"
            sheet["I1"].border = thin_border

        if self.type_sheet == "Type3":
            sheet["A1"].value = "ID"
            sheet["A1"].border = thin_border
            sheet["B1"].value = "Responsable"
            sheet["B1"].border = thin_border
            sheet["C1"].value = "Review"
            sheet["C1"].border = thin_border
            sheet["D1"].value = "Findings"
            sheet["D1"].border = thin_border
            sheet["E1"].value = "OK"
            sheet["E1"].border = thin_border            
            sheet["F1"].value = "Output Status OK"
            sheet["F1"].border = thin_border  
            sheet["G1"].value = "Comment (Optionnal)"
            sheet["G1"].border = thin_border        

        for col in range(1,sheet.max_column+1):
            for line in range(2,sheet.max_row+1):
                sheet.cell(row = line, column = col).border = thin_border

        exportFile = asksaveasfilename( defaultextension=".xlsx", filetypes=(("Excel file", "*.xlsx"),("All Files", "*.*") ))
        wb.save(exportFile)
        messagebox.showinfo("Information", "File successfully generated")

root = tk.Tk()
root.title("ID Extractor")
root.geometry("350x300")
app = Application(root)
app.mainloop()

这篇关于AttributeError:&#39;NoneType&#39;对象没有属性&#39;读取&#39;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Leetcode 234: Palindrome LinkedList(Leetcode 234:回文链接列表)
How do I read an Excel file directly from Dropbox#39;s API using pandas.read_excel()?(如何使用PANDAS.READ_EXCEL()直接从Dropbox的API读取Excel文件?)
subprocess.Popen tries to write to nonexistent pipe(子进程。打开尝试写入不存在的管道)
I want to realize Popen-code from Windows to Linux:(我想实现从Windows到Linux的POpen-code:)
Reading stdout from a subprocess in real time(实时读取子进程中的标准输出)
How to call type safely on a random file in Python?(如何在Python中安全地调用随机文件上的类型?)