how write with xlsxwriter in excel in a existing file without deleting the old data
XlsxWriter can only create new files. It cannot read or modify existing files.
There is a module called openpyxl that allows you to read and write to preexisting excel files.
Python xlsxwriter - add a worksheet to an existing workbook
I don't think this is still possible with xlsxwriter. Please take a look at this answer for help
Can't append Excel sheet with xlsxwriter
Actually i found that xlsxwriter can't append it cas only write (erase other sheets if exists).
The solution is to use openpyxl and this is an exemple of code :
import openpyxl
from openpyxl import Workbook # Module needed for creating new workbook
from openpyxl import load_workbook # Module needed for loading existing workbook
wb = load_workbook(filename)
ws = wb["sheetName"]
for row in dataframe_to_rows(dataframe, index=False, header=True):
ws.append(row)
wb.save(filename)
How to add a new work sheet to work book in xlsxwriter
Here is a sample code.
the workbook constructor needs to be created outside of the for loop and it does what you are looking for!
Input csv files:
SAMPLE CODE
import os
import glob
import xlsxwriter
import csv
flist = [os.path.basename(x) for x in glob.glob(os.getcwd() + '\\*.csv')]
workbook = xlsxwriter.Workbook('split_book.xlsx')
for sh in flist:
worksheet = workbook.add_worksheet(sh)
with open(sh, 'rb') as f:
reader = csv.reader(f)
for r, row in enumerate(reader):
for c, col in enumerate(row):
worksheet.write(r, c, col)
workbook.close()
OUTPUT
Related Topics
Python: How to Match Nested Parentheses With Regex
Compare a Column Between 2 CSV Files and Write Differences Using Python
How to Make a Discord Bot Leave a Server from a Command in Another Server
Grab a Number After a String in a File
How to Change the Title Bar in Tkinter
Change Date Formats in CSV With Python 3
Concatenate Two Columns in Csv: Python
Pandas.Read_Excel Parameter "Sheet_Name" Not Working
Remove Last Few Characters in Pyspark Dataframe Column
Python: How to Read and Load an Excel File from Aws S3
Remove Very First Row in Pandas
How to Write Multiple Images (Subplots) into One Image
How to Append Data Using Openpyxl Python to Excel File from a Specified Row
How to Read a List of Parquet Files from S3 as a Pandas Dataframe Using Pyarrow
Pandas Dataframe Check If Column Value Exists in a Group of Columns
Regex to Append Some Characters in a Certain Position
Converting Two Lists into a Matrix
When to Use Cla(), Clf() or Close() for Clearing a Plot in Matplotlib