[英]Copy and paste cells with python
I am trying to copy cells value from one workbook to another. 我试图将单元格值从一个工作簿复制到另一个工作簿。 I need to copy cells A1,A2,B4,B5,B6,B7 from Sheet2 and paste into new workbook in cells A1,B1,C1,D1,E1,F1. 我需要从Sheet2复制单元格A1,A2,B4,B5,B6,B7并粘贴到单元格A1,B1,C1,D1,E1,F1中的新工作簿中。 How can I do this with python? 我怎么能用python做到这一点?
import xlrd
file_location = "C:/attach/Source.xlsx"
workbook = xlrd.open_workbook(file_location)
sheet = workbook.sheet_by_index(1)
for row in range(sheet.nrows):
cell = sheet.cell(row, 1)
print(cell.value)
The process goes like this: 过程如下:
xlwt
: wb = xlwt.Workbook()
使用xlwt
创建一个新工作簿: wb = xlwt.Workbook()
ws = wb.add_sheet('Sheet1')
在新工作簿中创建一个新工作表: ws = wb.add_sheet('Sheet1')
ws.write(row, col, value)
将您的单元格复制到新工作表: ws.write(row, col, value)
You probably want to use a dictionary to map the cell position transform. 您可能希望使用字典来映射单元格位置变换。 It would look like this: {(row,col):(new_row, new_col), ...}
它看起来像这样: {(row,col):(new_row, new_col), ...}
To copy the cells in an existing workbook, you can use xlutils.copy.copy()
to copy the existing workbook and put it in a xlwt.Workbook object that you can then edit. 若要复制现有工作簿中的单元格,可以使用xlutils.copy.copy()
复制现有工作簿并将其放入xlwt.Workbook对象中,然后可以对其进行编辑。 The xlutils documentation describes this. xlutils文档描述了这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.