简体   繁体   English

Python-将数据从一个xlsx追加到另一个现有的xlsx

[英]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.

相关问题 Python-openpyxl使用公式在现有xlsx上写入后读取xlsx数据 - Python - openpyxl read xlsx data after writing on existing xlsx with formula Python:如何从一个XLSX中搜索一个字符串,使其位于另一个XLSX文件中? - Python: How do I search a string from one XLSX to be in another XLSX file? 从现有的xlsx导出python的图表 - python export chart from an existing xlsx 遍历目录 for.xlsx 文件,将每个文件中的一张表中的数据附加到 dataframe - Looping through a directory for .xlsx files appending data from one sheet in each file to a dataframe 如何通过 uniqueid 从 xlsx 文件中提取数据并使用 Python 将该数据写入另一个具有相同列名的 xlsx 文件? - How can I pull data by uniqueid from an xlsx file and write that data to another xlsx file with the same column name using Python? 从xlsx复制到另一个xlsx中的特定工作表 - Copying from xlsx to an specific sheet in another xlsx 用 python 覆盖 XLSX 中的数据 - Overwriting data in XLSX with python 如何在 Python 中处理来自 xlsx 文件的数据 - How to handle data from xlsx file in Python 如何使用Python将CSV数据复制到现有的xlsx文件 - How to copy CSV data to an existing xlsx file using Python 如何使用python从xlsx文件中的另一列中使用单元格值从一列中减去单元格值 - How to subtract cell values from one column with cell values from another column in xlsx files using python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM