将 Excel 与 Python 连接

发布于 2024-11-28 09:04:14 字数 742 浏览 2 评论 0原文

使用下面的代码,我可以获取要打印的数据。 如何将代码切换到 xlrd?

如何修改此代码以使用已打开且可见的 xls 文件。 因此,首先手动打开文件,然后运行脚本。

并且,得到更新。

然后推入Mysql

import os
from win32com.client import constants, Dispatch
import numpy as np

#----------------------------------------
# get data from excel file
#----------------------------------------
XLS_FILE = "C:\\xtest\\example.xls"
ROW_SPAN = (1, 16)
COL_SPAN = (1, 6)
app = Dispatch("Excel.Application")
app.Visible = True
ws = app.Workbooks.Open(XLS_FILE).Sheets(1)
xldata = [[ws.Cells(row, col).Value 
              for col in xrange(COL_SPAN[0], COL_SPAN[1])] 
             for row in xrange(ROW_SPAN[0], ROW_SPAN[1])]
#print xldata
a = np.asarray(list(xldata), dtype='object')
print a

Using code below, I can get the data to print.
How would switch code to xlrd?

How would modify this code to use a xls file that is already open and visible.
So, file is open first manually, then script runs.

And, gets updated.

and then get pushed into Mysql

import os
from win32com.client import constants, Dispatch
import numpy as np

#----------------------------------------
# get data from excel file
#----------------------------------------
XLS_FILE = "C:\\xtest\\example.xls"
ROW_SPAN = (1, 16)
COL_SPAN = (1, 6)
app = Dispatch("Excel.Application")
app.Visible = True
ws = app.Workbooks.Open(XLS_FILE).Sheets(1)
xldata = [[ws.Cells(row, col).Value 
              for col in xrange(COL_SPAN[0], COL_SPAN[1])] 
             for row in xrange(ROW_SPAN[0], ROW_SPAN[1])]
#print xldata
a = np.asarray(list(xldata), dtype='object')
print a

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花心好男孩 2024-12-05 09:04:14

如果您的意思是要修改当前文件,我 99% 确定这是不可能的,并且 100% 确定这是一个坏主意。为了更改文件,您需要具有写入权限。 Excel 创建文件锁以防止异步和同时编辑。如果在 Excel 中打开文件,那么唯一应该修改该文件的是... Excel。

如果您的意思是要读取编辑器中当前的文件,那么这是可能的——您通常可以获得对正在使用的文件的读取权限,但这同样是不明智的——如果用户尚未保存,则用户将看到一组数据,而您将在磁盘上看到另一组数据。

虽然我不喜欢 VB,但对于这个应用程序来说,这是一个更好的选择——使用宏将数据直接从 Excel 插入 MySQL。就我个人而言,我会创建一个仅具有插入权限的用户,然后我会尝试 本教程

If you mean that you want to modify the current file, I'm 99% sure that is not possible and 100% sure that it is a bad idea. In order to alter a file, you need to have write permissions. Excel creates a file lock to prevent asynchronous and simultaneous editing. If a file is open in Excel, then the only thing which should be modifying that file is... Excel.

If you mean that you want to read the file currently in the editor, then that is possible -- you can often get read access to a file in use, but it is similarly unwise -- if the user hasn't saved, then the user will see one set of data, and you'll have another set of data on disk.

While I'm not a fan of VB, that is a far better bet for this application -- use a macro to insert the data into MySQL directly from Excel. Personally, I would create a user with insert privileges only, and then I would try this tutorial.

放血 2024-12-05 09:04:14

如果你想操作一个已经打开的文件,为什么不使用COM呢?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文