Mic*_*ski 5 python excel openpyxl
我已经学习 Python 几个月了,以便在工作中自动执行一些无聊的任务(主要是 Excel),并且在大多数情况下我都取得了成功。但是两个星期以来,我一直在尝试解决我在使用 openpyxl 时遇到的一个问题,同时使用从其他电子表格收集的数据填充每周报告。openpyxl 版本是 3.0.2 openpyxl.__version__,我使用的是带有内置 IDLE 的 Python 3.6.7。编辑:这是完整的代码https://pastebin.com/MrBZ5Usu。
该程序应该做的是:
现在,当我运行程序时,我没有收到错误报告。Python shell 重新启动。该文件在那里,但它是 0 字节,无法打开。我已经运行了一些测试,直到保存工作簿的那一刻,一切似乎都很好。调用时,新工作簿中的单元格会显示我放入其中的值。每条数据都采用所需的标准化格式/类型。当我打电话给wb.save(filename)方法,shell 重新启动。我尝试了不同的方法将数据放入单元格中(使用 f 字符串循环,使用预定义的单元格坐标列表循环,对单元格和数据进行硬编码)但无济于事。Shell 重新启动 - 0 字节的电子表格。我已经确保我机器上的每个模块都是最新的等等。我已经成功地将数据写入搁置模块,然后用另一个脚本提取它们。第二个脚本(只有几行,用相同的代码来填充单元格)成功保存了一个有效的工作簿,但前提是它是同一个文件。我试过在主程序中改变它,但没有保存选项(作为不同的文件,作为同一个文件,文件的一个shutil副本(!))授予成功。我的代码显然有问题(所有新手除外)s错误)但我不能把手放在上面。这是代码 - 有没有人有任何建议?根据要求,我可以提供基本上整个脚本(大约 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
Run Code Online (Sandbox Code Playgroud)
之后我只是保存工作簿。
我重建了我的代码,使其具有比结构特征更多的功能。我还将代码的填写部分拆分为 4 个函数。
通过这种方式,我没有遇到任何错误、崩溃或意外行为。工作簿按预期填充了数据且没有错误。不幸的是,我还不够先进,无法查明确切的原因。这是最终的代码:
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")'
Run Code Online (Sandbox Code Playgroud)