有没有一种使用 Python Openpyxl 在 Excel 中写入数据的巧妙方法?
Is there a neat way to write data in Excel using Python Openpyxl?
我之前出于不同的目的问过这个问题。从那以后我修改了这段代码。
我想将数据写入 Excel 文件是单独的行。行数和每行的数据是用户输入的。 Header 没问题。数据已写入,但格式很尴尬。第一行数据从 A2 打印到 D2。
第二行从 B3 打印到 E3。同样,第三行从 B4 打印到 F4,依此类推。
我想将第二行从 A3 打印到 D3,而不是从 B3 打印到 E3。我可以对这段代码做些什么来获得最佳解决方案?谢谢
from openpyxl import Workbook
#Creates an Excel file from the scratch
wb = Workbook() #object of Workbook type
print(wb.active.title)
print(wb.sheetnames)
wb['Sheet'].title="Report_Amount"
sh1 = wb.active #Activate the sheet
sh1['A1'].value = "Item" #Writing into the cell
sh1['B1'].value = "Quantity"
sh1['C1'].value = "Price($)"
sh1['D1'].value = "Amount($)"
askrow = input("how many rows of data do you want to add: ")
askrow = int(askrow)
column1 = sh1.max_column
row1 = askrow
print(f'no. of columns : {column1}')
print(f'no. of rows : {row1}')
for k in range (2,row1+1):
inputItem = input("Enter Item Name: ")
sh1.cell(row=k, column=k - 1).value = inputItem
inputQuantity = input("Enter Quantity: ")
inputQuantity = int(inputQuantity)
sh1.cell(row=k, column=k).value = inputQuantity
inputPrice = input("Enter Price: ")
inputPrice = int(inputPrice)
sh1.cell(row=k, column=k + 1).value = inputPrice
sh1.cell(row=k, column=k + 2).value = ((sh1.cell(row=k, column=k).value) * sh1.cell(row=k, column=k + 1).value)
print("file saved")
wb.save("C:\Users\Ricky\Desktop\FirstCreatedPythonExcel1.xlsx")
你的代码有两个问题
一个是 for 循环中的 Column 变量,它是一个常量,但你使用了行变量 k,另一个是项目数比用户输入少一个。
我已经修复了这两个问题并尝试简化您的代码
from openpyxl import Workbook
wb = Workbook() #object of Workbook type
print(wb.sheetnames)
wb['Sheet'].title="Report_Amount"
sh1 = wb.active #Activate the sheet
sh1['A1'].value = "Item" #Writing into the cell
sh1['B1'].value = "Quantity"
sh1['C1'].value = "Price($)"
sh1['D1'].value = "Amount($)"
askrow = int(input("how many rows of data do you want to add: "))
for k in range (2,askrow+2):
sh1.cell(row=k, column=1).value = input("Enter Item Name: ")
inputQuantity = int(input("Enter Quantity: "))
sh1.cell(row=k, column=2).value = inputQuantity
inputPrice = float(input("Enter Price: "))
sh1.cell(row=k, column=3).value = inputPrice
sh1.cell(row=k, column=4).value = inputPrice * inputQuantity
wb.save("FirstCreatedPythonExcel1.xlsx")
请改回文件路径,现在商品价格也可以浮点数了。
我之前出于不同的目的问过这个问题。从那以后我修改了这段代码。 我想将数据写入 Excel 文件是单独的行。行数和每行的数据是用户输入的。 Header 没问题。数据已写入,但格式很尴尬。第一行数据从 A2 打印到 D2。 第二行从 B3 打印到 E3。同样,第三行从 B4 打印到 F4,依此类推。 我想将第二行从 A3 打印到 D3,而不是从 B3 打印到 E3。我可以对这段代码做些什么来获得最佳解决方案?谢谢
from openpyxl import Workbook
#Creates an Excel file from the scratch
wb = Workbook() #object of Workbook type
print(wb.active.title)
print(wb.sheetnames)
wb['Sheet'].title="Report_Amount"
sh1 = wb.active #Activate the sheet
sh1['A1'].value = "Item" #Writing into the cell
sh1['B1'].value = "Quantity"
sh1['C1'].value = "Price($)"
sh1['D1'].value = "Amount($)"
askrow = input("how many rows of data do you want to add: ")
askrow = int(askrow)
column1 = sh1.max_column
row1 = askrow
print(f'no. of columns : {column1}')
print(f'no. of rows : {row1}')
for k in range (2,row1+1):
inputItem = input("Enter Item Name: ")
sh1.cell(row=k, column=k - 1).value = inputItem
inputQuantity = input("Enter Quantity: ")
inputQuantity = int(inputQuantity)
sh1.cell(row=k, column=k).value = inputQuantity
inputPrice = input("Enter Price: ")
inputPrice = int(inputPrice)
sh1.cell(row=k, column=k + 1).value = inputPrice
sh1.cell(row=k, column=k + 2).value = ((sh1.cell(row=k, column=k).value) * sh1.cell(row=k, column=k + 1).value)
print("file saved")
wb.save("C:\Users\Ricky\Desktop\FirstCreatedPythonExcel1.xlsx")
你的代码有两个问题 一个是 for 循环中的 Column 变量,它是一个常量,但你使用了行变量 k,另一个是项目数比用户输入少一个。
我已经修复了这两个问题并尝试简化您的代码
from openpyxl import Workbook
wb = Workbook() #object of Workbook type
print(wb.sheetnames)
wb['Sheet'].title="Report_Amount"
sh1 = wb.active #Activate the sheet
sh1['A1'].value = "Item" #Writing into the cell
sh1['B1'].value = "Quantity"
sh1['C1'].value = "Price($)"
sh1['D1'].value = "Amount($)"
askrow = int(input("how many rows of data do you want to add: "))
for k in range (2,askrow+2):
sh1.cell(row=k, column=1).value = input("Enter Item Name: ")
inputQuantity = int(input("Enter Quantity: "))
sh1.cell(row=k, column=2).value = inputQuantity
inputPrice = float(input("Enter Price: "))
sh1.cell(row=k, column=3).value = inputPrice
sh1.cell(row=k, column=4).value = inputPrice * inputQuantity
wb.save("FirstCreatedPythonExcel1.xlsx")
请改回文件路径,现在商品价格也可以浮点数了。