我用openpyxl编写了一个简单的程序,我只需打开.xlsx文件,将数据输入某个单元格,然后关闭程序并再次运行,在不同的单元格中输入数据,但是当我打开时。运行第二个程序后的xlsx。
我的假设是openpyxl每次再次打开时都清除整个.xlsx文件,有没有办法避免这种情况?
这是我的代码:
from openpyxl import Workbook
wb = Workbook()
dest_filename = 'teste.xlsx'
ws = wb.active
ws.title = "2017"
Row = int(input('row: '))
Column = int(input('column: '))
data = input('data: ')
ws.cell(row = Row, column = Column).value = data
wb.save(filename = dest_filename)
答案 0 :(得分:0)
您根本没有阅读过excel文件:
使用它来阅读现有的工作簿:
from openpyxl import Workbook,load_workbook
import os
dest_filename = 'teste.xlsx'
if os.path.isfile(dest_filename):
wb = load_workbook(filename = dest_filename)
else:
wb = Workbook()
ws = wb.active
ws.title = "2017"
Row = int(input('row: '))
Column = int(input('column: '))
data = input('data: ')
ws.cell(row = Row, column = Column).value = data
wb.save(filename = dest_filename)
输出: