如何迭代 win32com 中的工作表? [英] How can I iterate over worksheets in win32com?
本文介绍了如何迭代 win32com 中的工作表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我生成了一个包含大量工作表的 xlsx 文件,当我使用 Excel 手动打开它时,我想将我带到特定位置.此功能可以完成这项工作,但仅限于一张纸.如何将其应用于工作簿中的所有工作表?
I generate an xlsx file with lots of sheets and I want to take me at specific position when I open it manually with Excel. This function does the job but for one sheet only. How can I apply it to all of the sheets in workbook?
import win32com.client
def select_cell():
xl = win32com.client.gencache.EnsureDispatch('Excel.Application')
wb = xl.Workbooks.Open(r'H:\Files\1.xlsx')
ws = xl.ActiveSheet
ws.Range('B100').Select()
wb.Close(True)
xl.Quit()
select_cell()
我想做这样的事情:
import win32com.client
def select_cell():
xl = win32com.client.gencache.EnsureDispatch('Excel.Application')
wb = xl.Workbooks.Open(r'H:\Files\1.xlsx')
for ws in wb.Worksheets():
ws.Range('B100').Select()
wb.Close(True)
xl.Quit()
select_cell()
推荐答案
为了被带到新生成的文档中的特定单元格,必须同时执行这两个表达式:
In order to be taken to specific cell in newly generated document it is necessary to have both of these expressions executed:
ws.Range('k100').Value = 1
ws.Range('k100').Select()
要在工作簿的每个工作表中执行此操作:
To do it in every sheet of the workbook:
def select_cell():
xl = win32com.client.gencache.EnsureDispatch('Excel.Application')
wb = xl.Workbooks.Open(r'H:\Files1.xlsx')
for sh in wb.Sheets:
xl.Worksheets(sh.Name).Activate()
ws = xl.ActiveSheet
ws.Range('k100').Value = 1
ws.Range('k100').Select()
wb.Close(True)
xl.Quit()
上面的代码将带您进入书中每个工作表的 K100.
The code above will take you to K100 on every worksheet in the book.
这篇关于如何迭代 win32com 中的工作表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文