python - 试图将两个单独的 excel 工作表合并为一个

标签 python

<分区>

 import xlwt
 import xlrd
 import os.path
 import os
 print os.path.abspath(os.curdir) 

#Create workbook and worksheet

wbk = xlwt.Workbook()
sheet = wbk.add_sheet('Data_1')#name of the sheet in which new data is there

row = 0

f = xlrd.open_workbook('Data_1.xls')#workbook from where data is extracted
g = xlrd.open_workbook('Data_2.xls')#workbook from where data is extracted

sheet1 = f.sheet_by_index(0)
sheet2 = g.sheet_by_index(0)

for i in xrange(16):
temp = sheet1.row_values(i)
print temp
col = 0
for element in temp:        
    sheet.write(i,col,element)
    col += 1
i += 1

for j in xrange(16):
temp = sheet2.row_values(j)
print temp
col = 4
for elements in temp:
    sheet.write(j,col.element)
    col +=1
j+=1
 wbk.save('DATA.xls') #excel sheet name when saved in the folder

在执行时,我能够在我的控制台上获取 Data_1.xls 的数据,但是当它进入第二组 for 循环时,它给我错误如下:-

Traceback (most recent call last):
File "C:/Python26/combining2ExcelSheets.py", line 34, in <module>
sheet.write(j,col.element)
AttributeError: 'int' object has no attribute 'element'

还有一点,我的两个 Excel 工作表(Data_1 和 Data_2.xls)的第一列是相同的。如果在我新生成的 Excel 工作表中,我想根据各自的第一列值组合和组织我的数据。我应该采取什么行动才能获得所需的排序 excel 表?

最佳答案

打字错误!

替换:

sheet.write(j,col.element)

sheet.write(j,col,element)

关于python - 试图将两个单独的 excel 工作表合并为一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11151382/

相关文章:

python - 索引数组中的两组列

python - 声明大小未知的 2D numpy 数组

python - CCKeyDerivationPBKDF 线程安全吗?

python - pywinauto:占用多个应用程序窗口

python - 有条件地调用 R 中的特定列

python - 将聊天记录存储在关系数据库中

python - Pandas 条形图中奇怪的虚线

python - Flask - 不受当前上下文的限制

python - 检测 TCP 套接字上是否调用了 `connect()`

python - 在 Windows 上通过命令行运行 Jupyter