如何使用 Python 将变量的值分配给 Excel 单元格
How to Assign Value From a Variable to an Excel Cell with Python
我想将变量 game_type 的值分配给 excel 单元格,但我无法这样做:
import openpyxl
wb = openpyxl.load_workbook('Table1.xlsx')
Card13 = wb.active
print Card13
class private_table():
def __init__(self, game):
if game == 13:
game_type = game
Card13['A2'] = game_type
wb.save('Table1.xlsx')
print(game_type)
elif game == '21':
game_type = int(game)
print(game_type)
elif game == '27':
game_type = int(game)
print(game_type)
else:
print("Enter correct game type")
def noc(self, cards):
if cards == 13:
game_type = Card13['A2'].value
if game_type == '13':
num_of_cards = cards
print num_of_cards
return num_of_cards
else:
print ("Please select the correct number of cards")
return("Please select the correct number of cards")
elif cards == '21':
game_type = Card13['B1'].value
if game_type == '21':
num_of_cards = cards
print num_of_cards
return num_of_cards
else:
print ("Please select the correct number of cards")
return("Please select the correct number of cards")
elif cards == '27':
game_type = Card13['B1'].value
if game_type == '27':
num_of_cards = cards
print num_of_cards
return num_of_cards
else:
print ("Please select the correct number of cards")
return("Please select the correct number of cards")
From the documentation of openpyxl,你在Excel中是这样写的:
from openpyxl import Workbook
wb = Workbook()
# grab the active worksheet
ws = wb.active
# Data can be assigned directly to cells
ws['A1'] = 42
# Rows can also be appended
ws.append([1, 2, 3])
# Python types will automatically be converted
import datetime
ws['A2'] = datetime.datetime.now()
# Save the file
wb.save("sample.xlsx")
如果您决定使用 xlsxwriter,这是一些工作示例,适用于 Python 3。对于 Python 2,从 print ()
中删除括号以看起来像这样 - print wks2.name
.
import xlsxwriter
from xlsxwriter.utility import xl_rowcol_to_cell
wbk = xlsxwriter.Workbook('testing.xlsx')
wks = wbk.add_worksheet()
wks.write('A1', 'Hello world')
print (wks.name)
wks2 = wbk.add_worksheet()
print (wks2.name)
i = -1
for x in range(1, 1000, 11):
i+=1
cella = xl_rowcol_to_cell(i, 0) #0,0 is A1!
cellb = xl_rowcol_to_cell(i, 1)
cellc = xl_rowcol_to_cell(i, 2)
print (cella)
wks2.write(cella,x)
wks2.write(cellb,x*3)
wks2.write(cellc,x*4.5)
wbk.close()
您只需要在与 Python 相同的目录中有一个名为 testing.xlsx
的工作簿。然后你在第一个 sheet:
得到这个
和添加的 sheet:
这就是你 运行 代码 Python:
得到的结果
作品名称sheet来自print(wks.name)
和print(wks2.name)
.
下面是帮助我在 excel sheet.
中插入变量值的解决方案
import openpyxl
wb = openpyxl.load_workbook('Table1.xlsx')
Card13 = wb.active
print Card13
class private_table():
def __init__(self, game):
if game == 13:
game_type = game
Card13['B1'] = game_type
wb.save('Table1.xlsx')
print(game_type)
elif game == 21:
game_type = (game)
Card13['B1'] = game_type
wb.save('Table1.xlsx')
print(game_type)
elif game == 27:
game_type = (game)
wb.save('Table1.xlsx')
Card13['B1'] = game_type
print(game_type)
else:
print("Enter correct game type")
我想将变量 game_type 的值分配给 excel 单元格,但我无法这样做:
import openpyxl
wb = openpyxl.load_workbook('Table1.xlsx')
Card13 = wb.active
print Card13
class private_table():
def __init__(self, game):
if game == 13:
game_type = game
Card13['A2'] = game_type
wb.save('Table1.xlsx')
print(game_type)
elif game == '21':
game_type = int(game)
print(game_type)
elif game == '27':
game_type = int(game)
print(game_type)
else:
print("Enter correct game type")
def noc(self, cards):
if cards == 13:
game_type = Card13['A2'].value
if game_type == '13':
num_of_cards = cards
print num_of_cards
return num_of_cards
else:
print ("Please select the correct number of cards")
return("Please select the correct number of cards")
elif cards == '21':
game_type = Card13['B1'].value
if game_type == '21':
num_of_cards = cards
print num_of_cards
return num_of_cards
else:
print ("Please select the correct number of cards")
return("Please select the correct number of cards")
elif cards == '27':
game_type = Card13['B1'].value
if game_type == '27':
num_of_cards = cards
print num_of_cards
return num_of_cards
else:
print ("Please select the correct number of cards")
return("Please select the correct number of cards")
From the documentation of openpyxl,你在Excel中是这样写的:
from openpyxl import Workbook
wb = Workbook()
# grab the active worksheet
ws = wb.active
# Data can be assigned directly to cells
ws['A1'] = 42
# Rows can also be appended
ws.append([1, 2, 3])
# Python types will automatically be converted
import datetime
ws['A2'] = datetime.datetime.now()
# Save the file
wb.save("sample.xlsx")
如果您决定使用 xlsxwriter,这是一些工作示例,适用于 Python 3。对于 Python 2,从 print ()
中删除括号以看起来像这样 - print wks2.name
.
import xlsxwriter
from xlsxwriter.utility import xl_rowcol_to_cell
wbk = xlsxwriter.Workbook('testing.xlsx')
wks = wbk.add_worksheet()
wks.write('A1', 'Hello world')
print (wks.name)
wks2 = wbk.add_worksheet()
print (wks2.name)
i = -1
for x in range(1, 1000, 11):
i+=1
cella = xl_rowcol_to_cell(i, 0) #0,0 is A1!
cellb = xl_rowcol_to_cell(i, 1)
cellc = xl_rowcol_to_cell(i, 2)
print (cella)
wks2.write(cella,x)
wks2.write(cellb,x*3)
wks2.write(cellc,x*4.5)
wbk.close()
您只需要在与 Python 相同的目录中有一个名为 testing.xlsx
的工作簿。然后你在第一个 sheet:
和添加的 sheet:
这就是你 运行 代码 Python:
得到的结果作品名称sheet来自print(wks.name)
和print(wks2.name)
.
下面是帮助我在 excel sheet.
中插入变量值的解决方案import openpyxl
wb = openpyxl.load_workbook('Table1.xlsx')
Card13 = wb.active
print Card13
class private_table():
def __init__(self, game):
if game == 13:
game_type = game
Card13['B1'] = game_type
wb.save('Table1.xlsx')
print(game_type)
elif game == 21:
game_type = (game)
Card13['B1'] = game_type
wb.save('Table1.xlsx')
print(game_type)
elif game == 27:
game_type = (game)
wb.save('Table1.xlsx')
Card13['B1'] = game_type
print(game_type)
else:
print("Enter correct game type")