如何在 openpyxl 中每次使用时都换行?
How to I make a new line every use in openpyxl?
我正在尝试通过 python 程序设置一种简单的方法来标记我的业务记录,并且我在 excel sheet 部分使用模块 openpyxl 并且我想知道 我如何才能做到这一点,所以每次我使用该程序时,它都会使用 excel sheet[=16 中的下一行=].谢谢!
from openpyxl import Workbook
wb = Workbook()
# grab the active worksheet
ws = wb.active
item = raw_input('Item: ')
sold = raw_input('Sold for: ')
percentage = raw_input('Percentage (in decimals): ')
date = raw_input('Date of Sale: ')
customer = raw_input('Customer: ')
# Data can be assigned directly to cells
ws['B2'] = item
ws['C2'] = sold
ws['D2'] = percentage
ws['E2'] = date
ws['F2'] = customer
wb.save("sample.xlsx")
你可以在这里使用ws.max_row
。还要确保每次加载之前保存的文件而不是打开一个新文件。
import openpyxl
wb = openpyxl.load_workbook('sample.xlsx')
# grab the active worksheet
ws = wb.active
item = raw_input('Item: ')
sold = raw_input('Sold for: ')
percentage = raw_input('Percentage (in decimals): ')
date = raw_input('Date of Sale: ')
customer = raw_input('Customer: ')
# Data can be assigned directly to cells
input_row = ws.max_row + 1
ws['B{}'.format(input_row)] = item
ws['C{}'.format(input_row)] = sold
ws['D{}'.format(input_row)] = percentage
ws['E{}'.format(input_row)] = date
ws['F{}'.format(input_row)] = customer
wb.save("sample.xlsx")
您也可以考虑在此处实现 while 循环:
import openpyxl
enter_more = 'y'
while enter_more == 'y':
wb = openpyxl.load_workbook('sample.xlsx')
# grab the active worksheet
ws = wb.active
item = raw_input('Item: ')
sold = raw_input('Sold for: ')
percentage = raw_input('Percentage (in decimals): ')
date = raw_input('Date of Sale: ')
customer = raw_input('Customer: ')
# Data can be assigned directly to cells
input_row = ws.max_row + 1
ws['B{}'.format(input_row)] = item
ws['C{}'.format(input_row)] = sold
ws['D{}'.format(input_row)] = percentage
ws['E{}'.format(input_row)] = date
ws['F{}'.format(input_row)] = customer
wb.save("sample.xlsx")
enter_more = raw_input('Enter "y" to enter more data...').lower()
编辑:
正如@CharlieClark 在评论中提到的那样,您可以只使用 .append()
:
import openpyxl
wb = openpyxl.load_workbook('sample.xlsx')
# grab the active worksheet
ws = wb.active
item = raw_input('Item: ')
sold = raw_input('Sold for: ')
percentage = raw_input('Percentage (in decimals): ')
date = raw_input('Date of Sale: ')
customer = raw_input('Customer: ')
# Data can be assigned directly to cells
ws.append([None, item, sold, percentage, date customer])
wb.save("sample.xlsx")
我正在尝试通过 python 程序设置一种简单的方法来标记我的业务记录,并且我在 excel sheet 部分使用模块 openpyxl 并且我想知道 我如何才能做到这一点,所以每次我使用该程序时,它都会使用 excel sheet[=16 中的下一行=].谢谢!
from openpyxl import Workbook
wb = Workbook()
# grab the active worksheet
ws = wb.active
item = raw_input('Item: ')
sold = raw_input('Sold for: ')
percentage = raw_input('Percentage (in decimals): ')
date = raw_input('Date of Sale: ')
customer = raw_input('Customer: ')
# Data can be assigned directly to cells
ws['B2'] = item
ws['C2'] = sold
ws['D2'] = percentage
ws['E2'] = date
ws['F2'] = customer
wb.save("sample.xlsx")
你可以在这里使用ws.max_row
。还要确保每次加载之前保存的文件而不是打开一个新文件。
import openpyxl
wb = openpyxl.load_workbook('sample.xlsx')
# grab the active worksheet
ws = wb.active
item = raw_input('Item: ')
sold = raw_input('Sold for: ')
percentage = raw_input('Percentage (in decimals): ')
date = raw_input('Date of Sale: ')
customer = raw_input('Customer: ')
# Data can be assigned directly to cells
input_row = ws.max_row + 1
ws['B{}'.format(input_row)] = item
ws['C{}'.format(input_row)] = sold
ws['D{}'.format(input_row)] = percentage
ws['E{}'.format(input_row)] = date
ws['F{}'.format(input_row)] = customer
wb.save("sample.xlsx")
您也可以考虑在此处实现 while 循环:
import openpyxl
enter_more = 'y'
while enter_more == 'y':
wb = openpyxl.load_workbook('sample.xlsx')
# grab the active worksheet
ws = wb.active
item = raw_input('Item: ')
sold = raw_input('Sold for: ')
percentage = raw_input('Percentage (in decimals): ')
date = raw_input('Date of Sale: ')
customer = raw_input('Customer: ')
# Data can be assigned directly to cells
input_row = ws.max_row + 1
ws['B{}'.format(input_row)] = item
ws['C{}'.format(input_row)] = sold
ws['D{}'.format(input_row)] = percentage
ws['E{}'.format(input_row)] = date
ws['F{}'.format(input_row)] = customer
wb.save("sample.xlsx")
enter_more = raw_input('Enter "y" to enter more data...').lower()
编辑:
正如@CharlieClark 在评论中提到的那样,您可以只使用 .append()
:
import openpyxl
wb = openpyxl.load_workbook('sample.xlsx')
# grab the active worksheet
ws = wb.active
item = raw_input('Item: ')
sold = raw_input('Sold for: ')
percentage = raw_input('Percentage (in decimals): ')
date = raw_input('Date of Sale: ')
customer = raw_input('Customer: ')
# Data can be assigned directly to cells
ws.append([None, item, sold, percentage, date customer])
wb.save("sample.xlsx")