简体   繁体   English

xlrd,python中的xlwt(如何复制整行)

[英]xlrd,xlwt in python (how to copy an entire row)

Ideally i am copying an entire row from another excel worksheet. 理想情况下,我从另一个excel工作表复制整行。 So instead of showing that code, my problem starts from when i have that row in a list, how do i put it in another excel worksheet now? 因此,我的问题不是显示代码,而是从列表中的那一行开始,我现在如何将它放在另一个excel工作表中? I want it to look exactly the same. 我希望它看起来完全一样。 Goal: Take either of the data formats, and putting them in a blank excel worksheet in eg row 1. I would like this to be something fast, since i plan to scale it up to 10,000 rows. 目标:采用任何一种数据格式,并将它们放在例如第1行的空白excel工作表中。我希望这是快速的,因为我计划将其扩展到10,000行。

Data can be in either format: 数据可以采用以下任一格式:

data = [number:6842.0, xldate:41771.0, xldate:0.005555555555555556,
        text:u'Hello World']
data = [6842.0, 41771.0, 0.005555555555555556, u'Hello World']

My Code: 我的代码:

import xlwt
wb = xlwt.Workbook()
data = [6842.0, 41771.0, 0.005555555555555556, u'Hello World'] #either format
sheet1 = wb.add_sheet("MyData")
sheet1.write(1,0,data)
wb.save('C:\\Python27\\helloworld.xls')

This is how i am grabbing my data from an excel file: 这就是我从excel文件中获取数据的方法:

from xlrd import *
import xlwt

book = open_workbook(filename= 'C:\Users\ssheikh\Desktop\CopyFile.xls')
sh = book.sheet_by_index(0)
workbook = xlwt.Workbook()
sheet = workbook.add_sheet("MyData")

b = xldate.xldate_from_date_tuple((2014,5,12), 0) 
c = xldate.xldate_from_date_tuple((2014,5,13), 0)

mylist = []
listc = []
listd = []
for i in xrange(0,sh.nrows):
    if sh.cell_value(rowx=i, colx =1) == b:
        mylist.append(i)
        listd.append(sh.row_values(i))
        print 'the number', b ,'was found in row=', i, 'and column =0'


    if sh.cell_value(rowx=i,colx =1) == c:
        listc.append(i)
        listd.append(sh.row(i))

#mylist[0] #upper limit of my rows
#listc[-1] #lower limit of my rows
listd # this has all of my rows that i want to put in a new excel file

I don't see any write_row method in xlwt like there is row_values in xlrd . 我没有在xlwt中看到任何write_row方法,就像row_values中有row_values 一样 There is only sheet.write of the Worksheet object of or write_rich_text if need be. 如果需要,只有sheet.write对象的write_rich_textwrite_rich_text

for row_index, row in enumerate(listd):
   for col_index, cell_value in enumerate(row):
       sheet.write(row_index, col_index, cell_value)
workbook.save("myData.xls")

If there are special styles you can use the optional style argument of write . 如果有特殊样式,则可以使用write的可选style参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM