Openpyxl和隐藏/未隐藏的Excel工作表 [英] Openpyxl and Hidden/Unhidden Excel Worksheets

查看:2116
本文介绍了Openpyxl和隐藏/未隐藏的Excel工作表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码从制表符分隔的文本文件中读取数据,然后将其写入现有Excel工作簿中的指定工作表。变量workbook,write_sheet和text_file由用户输入

  tab_reader = csv.reader(text_file ,delimiter ='\t')
xls_book = openpyxl.load_workbook(filename = workbook)
sheet_names = xls_book.get_sheet_names()
xls_sheet = xls_book.get_sheet_by_name(write_sheet)
对于row_index,枚举中的行(tab_reader):
number = 0
col_number = first_col
while number< num_cols:
cell_tmp = xls_sheet.cell(row = row_index,column = col_number)
cell_tmp.value = row [number]
number + = 1
col_number + = 1
xls_book.save(workbook)

然而,当我在一个预先存在的工作簿上运行这个代码哪个工作表是一个隐藏的选项卡,输出取消隐藏选项卡。我认为原因是因为openpyxl不是修改文件,而是完全创建一个新的文件。有没有一个简单的方法来告诉python检查工作表是否被隐藏,然后根据条件是否满足而输出一个隐藏或不可见的表?



谢谢!

解决方案

我们目前不支持在openpyxl中隐藏工作表,所以这在读取文件时被忽略,因此丢失节省它我不认为添加它应该太难了。请在Bitbucket上提交功能请求。



[更新]



现在可以使用:

  ws.sheet_state ='hidden'

或者实际上是 xls_sheet.sheet_state ='hidden'在您的特定情况下。


I have the following code that reads data from a tab-delimited text file and then writes it to a specified worksheet within an existing Excel workbook. The variables "workbook", "write_sheet", and "text_file" are input by the user

tab_reader = csv.reader(text_file, delimiter='\t')
xls_book = openpyxl.load_workbook(filename=workbook)
sheet_names = xls_book.get_sheet_names()
xls_sheet = xls_book.get_sheet_by_name(write_sheet)
for row_index, row in enumerate(tab_reader):
        number = 0
        col_number = first_col
        while number < num_cols:
                cell_tmp = xls_sheet.cell(row = row_index, column = col_number)
                cell_tmp.value = row[number]
                number += 1
                col_number += 1
xls_book.save(workbook)

However when I run this code on a preexisting "workbook" in which "worksheet" is a hidden tab, the output unhides the tab. I think the reason is because openpyxl is not modifying the file but creating a new file entirely. Is there an easy way to tell python to check if the worksheet is hidden and then output a hidden or unhidden sheet based on whether or not the condition is satisfied?

Thanks!

解决方案

We currently don't support hiding worksheets in openpyxl so this is just ignored when reading the file and, therefore, lost when saving it. I don't think it should be too hard to add it. Please submit a feature request on Bitbucket.

[UPDATE]

The feature is now available:

ws.sheet_state = 'hidden'

Or actually xls_sheet.sheet_state = 'hidden' in your particular case.

这篇关于Openpyxl和隐藏/未隐藏的Excel工作表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆