如何从Excel中读取和提取数据,并使用Python将其粘贴到文本文件中的现有文本中?

我想自动创build几个文本文件,其中包括一些我已经存储在一个Excel文件中的信息。 excel文件中存储了大量的信息,每个主题有一行和多个单元格,每个主题的信息将被写入不同的文本文件中。 来自excel文件的数据应该被粘贴在写在文本文件中的文本之间(见下面的代码)。

我已经做了一个代码,在每个文本文件中写入我需要的其他信息,但是我不知道如何从excel中获取数据到文本文件:

import xlrd file = open("testfile.txt", "w") file.write("text1.\n") file.write("text2\n") file.write("text3\n") file.write("text4\n") wb = xlrd.open_workbook(r"C:Data.xls") 

我不知道如何继续执行代码,让它通过excel文件逐行循环提取一些数据,粘贴到文本文件的文本中,closures文本文件并打开一个新的文件一样。

所以文本文件应该看起来像这样:

 text1 copied data from excel1 text2 copied data from excel2 text3 copied data from excel3 

等等…

有人能帮我吗? 我很抱歉,如果这是基本的,我是新的python。 在Python 3.4.1中工作

我会这样做:

 import xlrd xlsfilename='test.xlsx' test = xlrd.open_workbook(xlsfilename) number_subjetcs=50 # assuming it is known, otherwise you need a 'foreach line' loop number_columns=3 # assuming it is known... for row in range(number_subjetcs): txtfilename = 'testfile' + str(row) + '.txt' with open(txtfilename, "w") as f: for col in range(number_columns): s1 = 'text' + str(col+1) + ' : ' f.write(s1) # assuming there is only 1 sheet in the excel file val = test.sheets()[0].cell(row,col).value s2 = str(val) + '\n' f.write(s2)