使用Python清除Excel内容

问题描述 投票:0回答:1

我想使用Python清除Excel文件的内容,并在最后用不同的值重写它。我想在 Excel 保持打开状态时执行此操作,并且在再次重写之前不想关闭。

import pandas as pd

Tab = pd.read_excel("abc.xls",sheet_name="Sheet1")
Col_A = Tab['Col A']

Excel中的Col_A目前有以下值。我想清除除标题“Col A”之外的这些值,并用新值更新它们。

旧价值观
100
200
300
400
500
新价值观
600
700
800
900
1000

我想在更新 A 列中的新值后保持 Excel 打开并保存文件。

python excel openpyxl xlwings
1个回答
1
投票

这是使用 Xlwings 的示例;

工作表的单元格 A1 到 A5 中包含 100 到 500 的值,增量为 100。
工作簿在 Excel 中打开,并显示此预先填充的工作表。
然后 Python 等待击键继续。
当按下某个键时,这些单元格会更新为列下 600 到 1000 的值,并实时覆盖现有值。然后再次等待按键。
当按下某个键时,Excel 文件将被保存并退出 Excel。

import xlwings as xw


with xw.App(visible=True) as app:
    wb = xw.Book('foo.xlsx')
    ws = wb.sheets["Sheet1"]

    input("Press any key to update values")

    ws['A1'].value = [[600], [700], [800], [900], [1000]]

    input("Press any key to Save file and exit")

    wb.save('foo.xlsx')

© www.soinside.com 2019 - 2024. All rights reserved.