首页 > 编程知识 正文

python怎么打开excel,python怎么打开excel文件

时间:2023-05-04 21:47:28 阅读:208633 作者:1773

Tyson

如果您想在Excel中使用该文件,并使用Python在Excel中打开它,我更喜欢subprocess.Popen()而不是subprocess.Call()。

若要在特定工作表中打开文件,应先保存在选定工作表中打开的工作簿,然后再在Excel中打开它。如果您的文件是一个.xlsx文件,那么最好的Python模块是“openpyxl”(当然,在我看来)。

以下代码在我的机器中运行良好(Windows 10、MS Excel 2016和Python 3.6.3):import openpyxl

import subprocess

filename = ""

wsToOpen = ""

# 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)

然后,在MS Excel中打开文件,文件正好位于“wsToOpen中指定的工作表中”

赞踩评论- 2020年3月31日 18:29

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。