首页 > 解决方案 > 以电子表格格式转换 python 输出

问题描述

在这里,我需要从用户那里获取两个值并将该输出保存为电子表格格式。我的代码的 mcve 是:

while True:
    a=input('enter the string')
    print(a)
    b=input('enter the number')
    print(b)

示例输出为:

enter the stringa
a
enter the number2
2
enter the stringb
b
enter the number4
4
enter the string

我需要将每个整数值分配给电子表格中的相应字符串

标签: python

解决方案


尝试使用 openpyxl 将输出保存在 excel 表中

from openpyxl import Workbook


book = Workbook()
sheet = book.active
a = input('enter the string')
print(a)
b = input('enter the number')
print(b)
sheet['A1'] = a
sheet['B1'] = b



book.save("test.xlsx")

推荐阅读