I need to edit an excel file but without overwriting the old content I'm using xlsxwriter to create the excel file but it does not have this option is there any solution ?
Asked
Active
Viewed 3,388 times
1 Answers
1
xlsxwriter module documentation states:
Module cannot read or modify existing Excel XLSX files.
There are other python modules that allow you modify existing .xlsx files. Since you mentioned that you had created an existing workbook using xlsxwriter, I've put an example below that creates a file using xlsxwriter then reopens, modifies and saves the same file using the openpyxl module (link to openpyxl documentation).
import xlsxwriter
import openpyxl
from openpyxl import Workbook, worksheet, load_workbook`
workbook = xlsxwriter.Workbook("test.xlsx")
worksheet = workbook.add_worksheet('Sheet')
worksheet.write('A1', 'This cell was written using xlsxwriter')
workbook.close()`
wb = openpyxl.load_workbook("test.xlsx")
ws = wb.active
ws['A2'] = 'This cell was written using openpyxl'`
wb.save("test.xlsx")

cdrrr
- 1,138
- 4
- 13
- 44

patrickjlong1
- 3,683
- 1
- 18
- 32
-
1can you tell me those other python modules please ? – ismael bouzoubaa Jul 28 '17 at 09:30
-
Yes. openpyxl (the one used in the example), xlrd and xlwt. I am only really familiar with openpyxl. There's a good Question on using xlrd to modify existing worksheets at this [link].(https://stackoverflow.com/questions/26957831/edit-existing-excel-workbooks-and-sheets-with-xlrd-and-xlwt) – patrickjlong1 Jul 28 '17 at 16:15