Openpyxl保存损坏/不可读的文件。没有错误报告,只有shell res

2024-05-16 02:30:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经学习Python好几个月了,可以在工作中自动化一些枯燥的任务(主要是Excel),而且我在大多数情况下都取得了成功。但是两个星期以来,我一直在尝试解决openpyxl遇到的一个问题,同时用从其他电子表格收集的数据填充周报。 根据openpyxl.__version__,openpyxl版本是3.0.2,我使用的是内置IDLE的python3.6.7。 编辑:这里是完整的代码https://pastebin.com/MrBZ5Usu。你知道吗

程序应该做的是:

  1. 从一周的报告中收集数据,并将其列在一个列表中
  2. 从电子表格中收集数据,日期作为关键字,小时作为dict的值
  3. 打开一个模板电子表格,用这些数据填充它并将其另存为一个文件。你知道吗

现在当我运行程序时,我没有收到错误报告。Python shell将重新启动。文件在那里,但它是0字节,无法打开。 我已经运行了一些测试,直到保存工作簿的那一刻,一切似乎都很好。调用时,新工作簿中的单元格将显示我在其中输入的值。每一条数据都是希望的、标准化的格式/类型。 当我调用wb.save(filename)方法时,shell将重新启动。 我尝试了不同的方法将数据放入单元格(使用f字符串循环,使用预定义的单元格坐标列表循环,对单元格和数据进行硬编码),但都没有效果。Shell重新启动-0字节电子表格。 我已经确保我机器上的每个模块都是最新的等等。 我成功地将数据写入一个shelve模块,然后用另一个脚本提取它们。 第二个脚本(只有几行,用相同的代码填充单元格)成功地保存了一个有效的工作簿,但前提是它是同一个文件。 我试着在主程序中更改它,但是没有保存选项(作为不同的文件,作为同一个文件,文件的一个shutil副本(!)获得成功。 很明显我的代码有问题(除了所有新手的错误),但我不能把手放在上面。这是密码-有人有什么建议吗?根据要求,我可以提供基本上完整的脚本(约120行)。你知道吗

endlist = load_workbook(f"Endlist_{monat1}_2019.xlsx", data_only=True)
endlistws = endlist.active


#creating an empty dict, to store the dates and hours from endlist
endlist_hrs = {}

#creating a dict with dates as keys and empty lists as values
for cell in endlistws['A']:
    if cell.value != None:
        if weeknum(dateConverter(cell.value)) == kw_num:
            if dateConverter(endlistws[f'A{cell.row}'].value) in endlist_hrs.keys(): #is the date already in the dict?
                pass # it is, so pass
            else:
                endlist_hrs[dateConverter((endlistws[f'A{cell.row}'].value))] = [] #its not, so add it
    else:
        pass #does not match

# iterating over keys in the endlist_hrs dict, checking the dates in A column - not the best solution, iterating every time over whole A column - to be upgraded
for key in endlist_hrs.keys():
    for cell in endlistws['A']:
        if cell.value != None:
            if dateConverter(cell.value) == key:
                endlist_hrs[key].append(czasownik(endlistws[f'J{cell.row}'].value))


endlist.close() #closing the endlist workbook

#creating a dict with dates as keys and sum of hours as values - ready to be inserted into cells in the Check workbook
full_endlist_data = {k:sum(v) for (k,v) in endlist_hrs.items()}

#copying the dailycheck workbook and producing the final output

faylneym = f"DC{kw_num}.xlsx"
paf = os.path.join(values['Browse0'], faylneym)

shutil.copy2(values['Browse1'], paf)

dcwb = load_workbook(paf, write_only=True)
dcws = dcwb['KW_XX']
dcws.title = str(kw)
dcwb.save(paf)
dcwb = load_workbook(paf)
dcws = dcwb.active

for x,y in enumerate(strdate, start=2):
    dcws[f'A{x}'].value = y
for x,y in enumerate(strdate, start=12):
    dcws[f'A{x}'].value = y
for x,y in enumerate(hours_from_eos2, start=2):
    dcws[f'E{x}'].value = y
for x,y in enumerate(full_endlist_data.values(), start=2):
    dcws[f'D{x}'].value = y

之后,我只是保存工作簿。你知道吗


Tags: 文件the数据inforvaluecellkeys
1条回答
网友
1楼 · 发布于 2024-05-16 02:30:04

我已经重建了我的代码,使其具有更多的功能性而不是结构性。我还将代码的填充部分拆分为4个函数。你知道吗

  1. 复制电子表格并给它起一个新名字。你知道吗
  2. 打开电子表格,用先前生成的列表(6项,给定工作中的一系列日期)填写,然后保存工作簿。你知道吗
  3. 打开工作簿,遍历包含6个项目的列表,并将它们放到相应的位置。正在保存工作簿。你知道吗
  4. 打开工作簿,遍历dict值并将它们放入计划的单元格中。正在保存工作簿。你知道吗

这样做,我不会遇到错误,崩溃或意外的行为。工作簿按预期填充数据且无错误。不幸的是,我没有足够的先进性来找出确切的原因。 以下是最终代码:

def newWB(source, target, kw_num):
    faylneym = f"DC{kw_num}.xlsx"
    paf = os.path.join(target, faylneym)
    shutil.copy2(source, paf)
    return paf



def filling1(wbpaf, strdata):
    try:
        dcwb = load_workbook(wbpaf)
        dcws = dcwb.active

        for x, y in enumerate(strdate, start=2):
            dcws[f'A{x}'].value = y
        for x, y in enumerate(strdate, start=12):
             dcws[f'A{x}'].value = y
        # for x, y in enumerate(hours_from_eos, start=2):
        #     dcws[f'E{x}'].value = y
        # for x, y in enumerate(endlist_hrs.values(), start=2):
        #     dcws[f'D{x}'].value = y
        dcwb.save(wbpaf)

    except:
        sg.Popup("Filling1 with data failed")

def filling2(wbpaf, hours_from_eos):
    try:
        dcwb = load_workbook(wbpaf)
        dcws = dcwb.active
        for x, y in enumerate(hours_from_eos, start=2):
            dcws[f'E{x}'].value = y
        dcwb.save(wbpaf)
    except:
        sg.Popup("Filling2 with data failed")

def filling3(wbpaf, endlist_hrs):
    try:
        dcwb = load_workbook(wbpaf)
        dcws = dcwb.active
        for x, y in enumerate(endlist_hrs.values(), start=2):
            dcws[f'D{x}'].value = y
        dcwb.save(wbaf)
    except:
        sg.Popup("Filling3 failed")'

相关问题 更多 >