使用Python打开多个Excel文件

我在一个目录中有多个excel文件,并且希望一次打开这些文件来执行写入操作(例如,在所有excel文件的第一行中写入“Hi”)。 Python有没有办法做到这一点?

如果你需要更多的控制,你可以做这样的事情(来自一个叫Jay的人的解决scheme,不知道他是否是原作者http://www.python-excel.org/

import xlwt x=1 y=2 z=3 list1=[2.34,4.346,4.234] book = xlwt.Workbook(encoding="utf-8") sheet1 = book.add_sheet("Sheet 1") sheet1.write(0, 0, "Display") sheet1.write(1, 0, "Dominance") sheet1.write(2, 0, "Test") sheet1.write(0, 1, x) sheet1.write(1, 1, y) sheet1.write(2, 1, z) sheet1.write(4, 0, "Stimulus Time") sheet1.write(4, 1, "Reaction Time") i=4 for n in list1: i = i+1 sheet1.write(i, 0, n) book.save("trial.xls") 

您可以使用:

 import csv with open('A.csv','w') as a, open ('B.csv', 'w') as b: a_reader = csv.reader(a) b_reader = csv.reader(b) ... 

现在你可以遍历这两个文件。

python模块fileinput也是有帮助的,它允许遍历多个作为parameter passing的文件的行。