import xlrd
for name, sheet_name in zip(filename, sheetnumber):
book = xlrd.open_workbook(name)
sheet = book.sheet_by_name(sheet_name)
for row in range(sheet.nrows):
for column in range(sheet.ncols):
print row, column
import openpyxl
import subprocess
filename = "<Full path to file>"
wsToOpen = "<Worksheet name>"
# Openthe xlsx file in Python
xlsx_file = openpyxl.load_workbook(strCamArq)
# List with all the worksheets in the workbook
lst_worksheets = xlsx_file.get_sheet_names()
# Change the active worksheet in the workbook
xlsx_file._active_sheet_index = lst_worksheets.index(wsToOpen)
# Save the workbook open in the chosen sheet
xlsx_file.save(filename)
#Open the workbook in MS Excel
subprocess.Popen([filename], shell=True)
使用xlrd库:
如果您想在Excel中使用该文件,并使用Python在Excel中打开它,我更喜欢subprocess.Popen()而不是subprocess.Call()。
若要在特定工作表中打开文件,应先保存在选定工作表中打开的工作簿,然后再在Excel中打开它。如果您的文件是一个.xlsx文件,那么最好的Python模块是“openpyxl”(当然,在我看来)。
以下代码在我的机器中运行良好(Windows 10、MS Excel 2016和Python 3.6.3):
然后,在MS Excel中打开文件,文件正好位于“wsToOpen中指定的工作表中”
相关问题 更多 >
编程相关推荐