在Excel xlswriter中附加行 [英] Appending rows in excel xlswriter

查看:48
本文介绍了在Excel xlswriter中附加行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个xls文件,在其中将一些用户输入写入单元格.到目前为止,该程序可以正常运行;它写第一行.但是,当我再次运行该程序时,它没有在第一个程序的顶部附加行.我试图了解如何使它在excel工作表中添加新行,并保存并关闭等

I have created an xls file in which I write some user inputs into the cells. So far so good, the program works; it writes the first line. But when I run again the program instead of appending the rows it writes on top of the first one. I'm trying to understand how to make it append a new row into the excel sheet save it and close it etc

import xlsxwriter

workbook = xlsxwriter.Workbook("test.xlsx",)
worksheet = workbook.add_worksheet()

row = 0
col = 0

worksheet.write(row, col,     'odhgos')
worksheet.write(row, col + 1, 'e/p')
worksheet.write(row, col + 2, 'dromologio')
worksheet.write(row, col + 3, 'ora')


row += 1
worksheet.write_string(row, col,     odigosou)
worksheet.write_string(row, col + 1, dromou)
worksheet.write_string(row, col + 2, dromologio)
worksheet.write_string(row, col + 3, ora)

workbook.close()

使用我创建的这段代码,我可以在文件中写入内容,但是如何使它在现有工作表中追加一行.我看过的所有教程,研究过的所有说明,都行不通;我显然做错了,但我无法发现它.

With this code I created I'm able to write in the file but how do I make it to append a row in the existing sheet. All tutorials I watched, all instructions I researched, just don't work; I'm doing something wrong obviously but I'm not able to spot it.

推荐答案

问题:...如何使其在现有工作表中追加一行

Question: ... how do I make it to append a row in the existing sheet

使用 openpyxl 的解决方案,例如:

Solution using openpyxl, for instance:

from openpyxl import load_workbook

new_row_data = [
    ['odhgos', 'e/p', 'dromologio', 'ora'],
    ['odigosou', 'dromou', 'dromologio', 'ora']]

wb = load_workbook("test/test.xlsx")
# Select First Worksheet
ws = wb.worksheets[0]

# Append 2 new Rows - Columns A - D
for row_data in new_row_data:
    # Append Row Values
    ws.append(row_data)

wb.save("test/test.xlsx")

使用Python测试:3.4.2-openpyxl:2.4.1-LibreOffice:4.3.3.2

Tested with Python: 3.4.2 - openpyxl: 2.4.1 - LibreOffice: 4.3.3.2

这篇关于在Excel xlswriter中附加行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆