如何使用Python将变量中的值指定给Excel单元格

如何使用Python将变量中的值指定给Excel单元格,python,excel,python-2.7,automation,worksheet,Python,Excel,Python 2.7,Automation,Worksheet,我想将变量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 Card1

我想将变量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")
,这是您在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")

如果您决定使用,这是一些工作示例,用于Python3。对于Python2,从
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
的工作簿。然后你可以在第一张纸上看到:

在增加的表格上:

这是使用Python运行代码时得到的结果:


工作表的名称来自
print(wks.name)
print(wks2.name)
下面是帮助我在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['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”的值写入单元格Card13['A2'],这对我不起作用。您对这一行有什么期望-
print Card13
?@vityta我想看看哪个是选中的活动工作表。@vityta是的,我得到了,是的,我正在使用python 2.7