Openpyxl 的“load_workbook”在自定义类导入时出现 badzipfile 错误 - python

问题描述 投票:0回答:0

这是我关于堆栈溢出的第一个问题,所以请耐心等待,让我知道我是否应该在某个地方更改我的问题

所以,我正在尝试使用

load_workbook
openpyxl
python
中打开一个excel文件。

我发现如果我导入一个我称为

Document
的自定义类,我会得到一个
bad zip file error
badzipfile:文件不是 zip 文件。 我可以从同一个脚本导入其他类。更改班级名称(使用
as
或实际班级名称)仍然会出现相同的错误。

两者从不一起使用 但只有以下内容会给我错误:

from some_script import Document

from some_script import Document as other_name

from some_script import *

import some_script

然后

doc = Document()  # some_script.Document(), etc.

仅删除那些行以某种方式消除了错误。

# this doesn't work either
class other_name: # instead of class Document:

现在我只是在没有我的

Document
课的情况下生活。

但问题是我不知道为什么它不起作用(在同一个脚本中上课也很方便)。

感谢任何帮助,希望代码不要太乱

这是我班级的完整代码,试图包含所有内容:

import datetime
import random
import time
import os
import traceback
from secrets import token_urlsafe
import requests
import ast
import helpful
from werkzeug.utils import secure_filename

class Document:
    this_id = ''
    process_id = ''
    #status = 'waiting'
    files = ''
    email = ''
    start_time = ''
    override_path = False
    def get_files(self) -> dict[str, File]:
        temp = ast.literal_eval(self.files)
        return_files = {k: File(temp[k].split('[s]')[0], temp[k].split('[s]')[1], self.override_path) for k in temp.keys()}
        return return_files
    this_file = None
    def __str__(self):
        return f'email: {self.email}, this_id: {self.this_id}, process_id: {self.process_id}, status: {self.get_status()}, this_file: {self.this_file}, files: {[str(k) + str(self.get_files()[k]) for k in self.get_files().keys()]}'
    def __init__(self, _process_id, _file_name, _fil_dir, _files, override_path = False):
        self.generate_id()
        self.process_id = _process_id
        self.this_file = File(_file_name, _fil_dir, override_path)
        self.email = _fil_dir
        self.start_time = helpful.get_now_str()
        self.override_path = override_path
        # for f in _files:
        #     self.files[f['tag']] = File(f['file_name'], f['dir'])
        self.files = '{ '+', '.join([f'"{f["tag"]}": "{f["file_name"]}[s]{f["dir"]}"' for f in _files])+' }'

        temp_files = self.get_files()
        for k in temp_files.keys():
            f = temp_files[k]
            f.save_from_url()


    def generate_id(self):
        if os.path.exists('document_ids.p'):
            self.this_id = helpful.unpickle('document_ids.p') + 1
        else:
            self.this_id = 1
        helpful.pickle(self.this_id, 'document_ids.p')

    def get_editing_path(self):
        return self.status_path()+'.txt'

    def status_path(self):
        if not os.path.exists('data'):
            os.makedirs('data')
        return os.path.join('data', f'document_{self.process_id}_{self.this_id}.p')

    def get_status(self):
        # while helpful.read(self.get_editing_path()) == 'editing':
        #     pass
        # #try:
        # helpful.write(self.get_editing_path(), 'editing')
        if not os.path.exists(self.status_path()):
            self.set_status('waiting')

        @helpful.one_at_a_time(location=self.this_file.name, time_limit=0.5)
        def _get_status():
            _status = helpful.unpickle(self.status_path())
            return _status
        # helpful.write(self.get_editing_path(), 'none')
        return _get_status()
        #finally:
        # helpful.write(self.get_editing_path(), 'none')

    def processing(self):
        status = self.get_status()
        is_processing = 0  # False
        for s in ['timed out', 'error_logged', 'waiting', 'done']:
            is_processing += status == s  # True == 1
        return is_processing > 0

    def set_status(self, _status):
        @helpful.one_at_a_time(location=self.this_file.name, time_limit=0.5)
        def _set_status():
            helpful.pickle(_status, self.status_path())
        _set_status()
        requests.post(url+'/api/update-status',
                      headers={'key': key, 'email': self.email,
                               'doc_status': _status}, json={'file_name': self.this_file.name, 'process_id': self.process_id})

    def delete_self(self):
        remove_process_from_list(self.this_id)
        files = self.get_files()
        for f in [files[k] for k in files.keys()]:#+[self.this_file]:
            if f:
                f.delete()
                f.delete_from_url()
        if self.this_file:
            self.this_file.delete()
        if os.path.exists(self.status_path()):
            os.remove(self.status_path())
        if os.path.exists(self.get_editing_path()):
            os.remove(self.get_editing_path())
        # self.delete_from_server()

    def get_disk_space_of_files(self):
        size = 0
        temp_files = self.get_files()
        for k in temp_files.keys():
            f = temp_files[k]
            if not os.path.exists(f.location()):
                f.save_from_url()
            if os.path.exists(f.location()):
                size += os.stat(f.location()).st_size
        return (size / 1024.0) / 1024.0  # MB

    def delete_from_server(self):
        max_time = datetime.timedelta(seconds=20.0)
        done = False
        og_now = helpful.get_now()
        while not done:
            try:
                r = requests.get(url+'/api/remove-document-to-process',
                                 headers={'key': key},
                                 json={'remove': convert_to(self.this_file.name)})
                done = r.json()['success'] == 'true'
            except Exception as e:
                print(e)
                print(traceback.print_exc())
            time.sleep(1)
            if og_now < helpful.get_now() - max_time:
                done = True


def convert_to(real_path):
    return os.path.normpath(real_path).replace(os.sep, '|')


def convert_from(encoded_path):
    return os.path.join(*encoded_path.split('|'))


def read_in_chunks(file_object, chunk_size=524288):
    while True:
        data = file_object.read(chunk_size)
        if not data:
            break
        yield data
python openpyxl
© www.soinside.com 2019 - 2024. All rights reserved.