-2

我的openpyxl有问题,当我想启动脚本时出现此错误,直到昨天它才起作用,现在没有了,我试图卸载该模块,但问题仍然存在,我删除了它的excel文件并且无处可去打开。有任何想法吗?

import openpyxl
from openpyxl import Workbook
from openpyxl import load_workbook
from openpyxl.styles import Border, Side, PatternFill, Font, GradientFill, Alignment
from openpyxl.styles import colors
from openpyxl.cell import Cell
from termcolor import colored, cprint
from openpyxl.styles import numbers

from os import mkdir
myPath = '.\Erstellte Datein' # initialize the Chrome driver

def excel():
                
                # Writing on a EXCEL FILE
                filename = (f"{myPath}/Monatsplan openpytesst.xlsx")
                dienstorinfo = 'texttest'
                emptycell = ' '   
                x = len(dienstorinfo)
                if x == 0:
                    dienstorinfo = tagesinfo2
                try:
                    wb = load_workbook(filename)
                    ws = wb.worksheets[0]  # select first worksheet
                except FileNotFoundError:
                    headers_row = ['Datum','Dienst','Funktion','Von','Bis','Schichtdauer','Bezahlte Zeit','Überzeit','Sonnats Zulage','Nachtdienst']
                    wb = Workbook()
                    ws = wb.active
                wb.save(filename)
                ws.append(['1','2','2','4','5'])
                wb.close()

                for cols in ws.iter_cols(  ):
                     if cols[-1].value:
                        cols[-1].border = Border(left=Side(style='thin'),right=Side(style='thin'),top=Side(style='thin'),bottom=Side(style='thin'))                                
                ws.column_dimensions['A'].width = 11
                ws.row_dimensions['1'].height = 25
                ws.column_dimensions['B'].width = 60   
                ws.column_dimensions['C'].width = 2   
                ws.column_dimensions['D'].width = 3   
                ws.column_dimensions['E'].width = 3   
                ws.column_dimensions['F'].width = 3   
                ws.column_dimensions['H'].width = 3        
                ws.column_dimensions['I'].width = 2   
                ws.column_dimensions['L'].width = 2   
                wb.save(filename)              
                wb.close()
excel()

在此处输入图像描述

4

1 回答 1

0

要么是您的 .xlsx 文件已损坏,要么是您引用了错误的文件。

获取一个新的 .xlsx 文件或查找未损坏的文件并开始工作,这对我有用。

于 2022-02-20T22:04:47.257 回答