简体   繁体   English

无法通过openpyxl使用Python打开excel文档

[英]unable to open excel document using Python through openpyxl

I'm new to Python, so sorry if this is annoyingly simple.我是 Python 的新手,如果这很烦人,很抱歉。 I'm trying to simply open an excel document using this,我试图用这个简单地打开一个excel文档,

 import openpyxl from openpyxl.reader.excel import load_workbook wb = openpyxl.load_workbook('C:\\Users\\ my file location here.xlsx') #with my real location

I didn't get any error, however i don't understand why file doesn't open ?我没有收到任何错误,但是我不明白为什么文件打不开?

The location of the file is correct as I can open it using,文件的位置是正确的,因为我可以使用打开它,

 file = "C:\\Users\\ my file location here.xlsx" # os.startfile(file)

Thanks谢谢

1) Try this first. 1)先试试这个。

import openpyxl
wb = openpyxl.load_workbook('C:\Users\ my file location here.xlsx')
type(wb)

2) else put your .py file in the same directory where .xlsx file is present and change code in .py as shown below. 2)否则将您的 .py 文件放在 .xlsx 文件所在的同一目录中,并更改 .py 中的代码,如下所示。

import openpyxl
wb = openpyxl.load_workbook('urfilename.xlsx')
type(wb)

you incorrectly invoke the load_workbook function您错误地调用了 load_workbook 函数

wb = openpyxl.load_workbook('C:\\Users\\ my file location here.xlsx') #with my real location

you should use just load_workbook你应该只使用 load_workbook

wb = load_workbook('C:\\Users\\ my file location here.xlsx') #with my real location

我使用/而不是\\作为文件夹分隔符让它工作。

wb = openpyxl.load_workbook('C:/Users/my file location here.xlsx')

It sounds like you want to open the file in Excel .听起来您想在 Excel 中打开文件。 That is, you want to launch the Excel application, and have the file open in that application.也就是说,您想要启动 Excel 应用程序,并在该应用程序中打开文件。 OpenPyXL and xlrd are not designed to do this. OpenPyXL 和 xlrd 并非设计用于执行此操作。 In fact, they are specifically designed for working with the files when you can't or don't want to launch Excel.事实上,它们是专门为在您不能或不想启动 Excel 时处理文件而设计的。 (For example, OpenPyXL and xlrd both work on Linux machines, which can't even run Excel.) (例如,OpenPyXL 和 xlrd 都可以在 Linux 机器上运行,甚至不能运行 Excel。)

You probably want some kind of Excel automation, like you would get with VBA or VBScript or a .NET language, except you want to do it in Python.您可能需要某种 Excel 自动化功能,就像使用 VBA 或 VBScript 或 .NET 语言一样,除非您想用 Python 来实现。 If so, the package you are looking for is xlwings .如果是这样,您正在寻找的包是xlwings

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM