excel工作表在完成脚本后被标记为组

k7fdbhmy  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(378)

在我的脚本对excel文件做了一些处理之后,他处理的工作表仍然标记为一个组,我找不到任何原因。有人能帮忙吗?
我的代码很长,所以我只分享一些部分:

import ...

wbName = 'Kaprosinec2020.xlsx'

df2 = pd.read_excel(wbName, sheet_name='Kalibrace')
df4 = pd.read_excel(wbName, sheet_name='Vypocet Ka')

# modifies dataframes df2 and df4 (to simplyfy this, I let them both under same names here, original script renames them of course after he make those changes...)

book = load_workbook(wbName)
writer = pd.ExcelWriter(wbName, engine='openpyxl')
writer.book = book
writer.sheets = dict((ws.title, ws) for ws in book.worksheets)
df2.to_excel(writer, 'Kalibrace', index=False, header=None, startcol=0, startrow=1)
df4.to_excel(writer, 'Vypocet Ka', index=False, header=None, startcol=9, startrow=3)
writer.save()
writer.close()

# now I used diferent type of writer, because I couldn't find a way to do so with the previous one...

# don't laugh at me, still a beginner trying to help myself any way nessesary... :)

wb = openpyxl.load_workbook(wbName)
wb.active = 1   # 'Kalibrace' is now an active sheet
ws = wb.active

# does some stuff...

wb.active = 0   # 'Vypocet Ka' is now an active sheet
ws = wb.active

# does some stuff...

wb.save(wbName)
wb.close

现在,当我打开结果excel文件时,两张表“kalibrace”和“vypocet ka”被标记为一个组,所以当我想进一步修改它们时,我必须先取消它们的标记,有点不兼容-脚本可以为我这样做吗?
我正在共享一张图片:结果excel文件的图片
更新:已解决。:)将其中一个活动工作表与下面的行取消标记即可实现此目的。

wb['Kalibrace'].views.sheetView[0].tabSelected = False

因为我没有足够的声誉来标记评论是有用的,所以我要感谢warcupine这样的回答。

hs1ihplo

hs1ihplo1#

问题是 wb.active 将选择一个工作表而不取消选择当前工作表。一起选择的两张图纸被分组。
因此,如果你只有两张纸,这应该可以:

wb['Kalibrace'].views.sheetView[0].tabSelected = False

如果您计划使用两个以上,您可以取消选择他们所有。

for sheet in wb:
    wb[sheet.title].views.sheetView[0].tabSelected = False

相关问题