![从一个文件(.xlsx)读取数据,写入另一个文件](https://linux22.com/image/1616542/%E4%BB%8E%E4%B8%80%E4%B8%AA%E6%96%87%E4%BB%B6%EF%BC%88.xlsx%EF%BC%89%E8%AF%BB%E5%8F%96%E6%95%B0%E6%8D%AE%EF%BC%8C%E5%86%99%E5%85%A5%E5%8F%A6%E4%B8%80%E4%B8%AA%E6%96%87%E4%BB%B6.png)
我正在尝试从 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