[英]Python - Appending data from one xlsx to another existing xlsx
I have gone through many existing questions about the same, but didn't find any satisfactory answer to my problem. 我已经解决了许多有关相同问题的现有问题,但是没有找到令人满意的答案。
Here's a bunch of code for appending values from xlsx to existing xlsx: 这是一堆用于将xlsx的值附加到现有xlsx的代码:
from xlutils.copy import copy
import xlrd
wb = xlrd.open_workbook("cik_list.xlsx")
sheet = wb.sheet_by_index(0)
X0 = []; X1 = []; X2 = []; X3 = []; X4 = []; X5 = []
for row in range(1, sheet.nrows):
X0.append(sheet.cell_value(row, 0))
X1.append(sheet.cell_value(row, 1))
X2.append(sheet.cell_value(row, 2))
X3.append(sheet.cell_value(row, 3))
X4.append(sheet.cell_value(row, 4))
X5.append(sheet.cell_value(row, 5))
rb = xlrd.open_workbook("Output Data Structure.xlsx")
r_sheet = rb.sheet_by_index(0)
r = sheet.nrows
wb1 = copy(rb)
sheet1 = wb1.get_sheet(0)
row=1
while(row < r):
row=1
for x in X0:
sheet1.write(row, 0, x)
row+=1
row=1
for x in X1:
sheet1.write(row, 1, x)
row+=1
row=1
for x in X2:
sheet1.write(row, 2, x)
row+=1
row=1
for x in X3:
sheet1.write(row, 3, x)
row+=1
row=1
for x in X4:
sheet1.write(row, 4, x)
row+=1
row=1
for x in X5:
sheet1.write(row, 5, x)
row+=1
wb1.save("Output Data Structure.xls")
Is there any way out I can save the Output Data Structure
as .xlsx
file without changing the first half of the code ie reading the values from cik_list.xlsx
and storing them into 6 different lists. 有什么办法可以将
Output Data Structure
另存为.xlsx
文件,而无需更改代码的前半部分,即从cik_list.xlsx
读取值并将它们存储到6个不同的列表中。
Thanks 谢谢
With a few changes in the lower half of the code, I got it working absolutely fine. 在代码的下半部分进行了一些更改,我就可以正常工作了。
from openpyxl import load_workbook
rb = load_workbook("Output Data Structure.xlsx")
sheet1 = rb.active
r = sheet.nrows
row=2
while(row <= r):
row=2
for x in X0:
sheet1.cell(row, 1, x)
row+=1
row=2
for x in X1:
sheet1.cell(row, 2, x)
row+=1
row=2
for x in X2:
sheet1.cell(row, 3, x)
row+=1
row=2
for x in X3:
sheet1.cell(row, 4, x)
row+=1
row=2
for x in X4:
sheet1.cell(row, 5, x)
row+=1
row=2
for x in X5:
sheet1.cell(row, 6, x)
row+=1
rb.save("Output Data Structure.xlsx")
Any further improvements would be highly appreciable. 任何进一步的改进都是非常可取的。
Thanks 谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.