xlsxwriter: How to insert a new row

孤人 提交于 2020-08-27 07:12:02

问题


Using xlsxwriter, how do I insert a new row to an Excel worksheet? For instance, there is an existing data table at the cell range A1:G10 of the Excel worksheet, and I want to insert a row (A:A) to give it some space for the title of the report.

I looked through the documentation here http://xlsxwriter.readthedocs.io/worksheet.html, but couldn't find such method.

import xlsxwriter

# Create a workbook and add a worksheet.
workbook = xlsxwriter.Workbook('Expenses01.xlsx')
worksheet = workbook.add_worksheet()
worksheet.insert_row(1)  # This method doesn't exist

回答1:


Unfortunately this is not something xlsxwriter can do.

openpyxl is a good alternative to xlsxwriter, and if you are starting a new project do not use xlsxwriter.

Currently openpyxl can not insert rows, but here is an extension class for openpyxl that can.

openpyxl also allows reading of excel documents, which xlsxwriter does not.




回答2:


By using openpyxl you can insert iew rows and columns

import openpyxl

file = "xyz.xlsx"
#loading XL sheet bassed on file name provided by user
book = openpyxl.load_workbook(file)
#opening sheet whose index no is 0
sheet = book.worksheets[0]

#insert_rows(idx, amount=1) Insert row or rows before row==idx, amount will be no of 
#rows you want to add and it's optional
sheet.insert_rows(13)

Hope this helps




回答3:


you should usewrite

read this: set_column(first_col, last_col, width, cell_format, options)

for example:

import xlsxwriter
workbook =xlsxwriter.Workbook('xD.xlsx')
worksheet = workbook.add_worksheet()
worksheet.write(row, col,     'First Name')
workbook.close()



回答4:


I am very much unhappy with the answers. The library xlxsWriter tends to perform most of the operations easily. To add a row in the existing worksheet , you can

    wb.write_row(rowNumber,columnNumber,listToAdd)


来源:https://stackoverflow.com/questions/36957035/xlsxwriter-how-to-insert-a-new-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!