我正在尝试从 input.xlsx 文件中读取所有行和列的数据,然后使用 python 脚本(遍历 for 循环)将其插入到另一个文件 output.xlsx 中。
问题: 此脚本中使用“for 循环”可以读取每行和每列的数据,但无法写入输出文件 (output.xlsx)
这是我使用的代码:
response = requests.get(url, params=params, auth=(username, password))
with open('/Downloads/input.xlsx', 'wb') as f:
f.write(response.content)
workbook = load_workbook("/Downloads")
# get all worksheets
all_sheets = workbook.sheetnames
first_sheet = all_sheets[0]
# get a worksheet by its name
worksheet = workbook[first_sheet]
first_row = True
for val in worksheet.values:
if first_row:
# skip the first row: header row.
first_row = False
continue
workbook = xlsxwriter.Workbook('output.xlsx')
new_worksheet = workbook.add_worksheet()
new_worksheet.write('A', val[0])
new_worksheet.write('B', val[1])
new_worksheet.write('C', val[2])
workbook.close()
input.xlsx 中的数据如下所示:
**Student Name | Course | Rank**
Tony ECE 4
Bob CSE. 1
Chris MECH 7
David CSE 5
Krishna CSE 2