使用Python实时更新Excel工作表 [英] Update an Excel sheet in real time using Python

查看:773
本文介绍了使用Python实时更新Excel工作表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Excel中打开电子表格时,是否可以实时更新?我有一个名为Example.xlsx的工作簿,该工作簿已在Excel中打开,并且我有以下python代码尝试使用字符串'ID'更新单元格B1:

Is there a way to update a spreadsheet in real time while it is open in Excel? I have a workbook called Example.xlsx which is open in Excel and I have the following python code which tries to update cell B1 with the string 'ID':

import openpyxl

wb = openpyxl.load_workbook('Example.xlsx')
sheet = wb['Sheet']
sheet['B1'] = 'ID'

wb.save('Example.xlsx')

在运行脚本时出现此错误:

On running the script I get this error:

PermissionError: [Errno 13] Permission denied: 'Example.xlsx'

我知道它是因为该文件当前已在Excel中打开,但是我想知道打开它时是否还有其他方法或模块可以用来更新工作表.

I know its because the file is currently open in Excel, but was wondering if there is another way or module I can use to update a sheet while its open.

推荐答案

我实际上已经使用xlwings弄清了这一点,并且非常简单.下面的代码打开一个名为Example.xlsx的现有Excel文件并实时更新,在这种情况下,运行脚本后立即将B45单元中的值放入45.

I have actually figured this out and its quite simple using xlwings. The following code opens an existing Excel file called Example.xlsx and updates it in real time, in this case puts in the value 45 in cell B2 instantly soon as you run the script.

import xlwings as xw

wb = xw.Book('Example.xlsx')
sht1 = wb.sheets['Sheet']
sht1.range('B2').value = 45

这篇关于使用Python实时更新Excel工作表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆