Using openpyxl cannot create XLSX - python

I tried the below code. I tried opening on Excel but I was getting that the file is damaged and then repaired the file to read it. How do I remove the issue of xlsx repair when xlsx is opened.
import csv
from openpyxl import Workbook
def unicode_csv_reader(utf8_data, dialect=csv.excel, **kwargs):
csv_reader = csv.reader(utf8_data, dialect=dialect, **kwargs)
for row in csv_reader:
yield [unicode(cell, 'utf-8') for cell in row]
filename = 'WS_Data_0_2019_09_25_215340128516.csv'
reader = unicode_csv_reader(open(filename))
wb = Workbook()
ws = wb.active
# lets just save 10 rows in a xlsx
i = 0
for row in reader:
if i < 10:
ws.append(row)
# print row
i = i+1
wb.save('example.xlsx')

Related

openpyxl load_workbook method return only the first sheet in a xlsx file - pandas

I created this method to add a new sheet to an empty workbook based on another workbook with more than one sheet:
def add_sheet(self, file_blob, df, sheetname, get_random_string):
try:
path= self.store(file_blob=file_blob,filename=f'{get_random_string(4)}.xlsx')
new_path = self.store(file_blob=io.BytesIO(), filename=f'{get_random_string(4)}_new.xlsx')
# Open the existing workbook
empty_wb = Workbook()
# Save the workbook to a file
empty_wb.save(new_path)
wb1 = load_workbook(path,read_only=True)
for sheet in wb1.worksheets:
print(sheet)
wb2= load_workbook(new_path)
# Create a new sheet
wb2.remove(wb2["Sheet"])
for ws_wb1 in wb1.worksheets:
ws_wb2 = wb2.create_sheet(ws_wb1.title)
for row in ws_wb1:
for cell in row:
if cell.value:
ws_wb2[cell.coordinate].value= cell.value
else:
pass
new_sheet = wb2.create_sheet(title=sheetname)
# Write the DataFrame to the new sheet
new_sheet.append(list(df.columns))
for row in df.values:
new_sheet.append(list(row))
# Save the workbook
wb2.save(new_path)
except Exception as err:
logging.error("Unexpected error %s: %d ", err, type(err))
raise
else:
with open(new_path, 'rb') as file:
byte_content = file.read()
return io.BytesIO(byte_content)
however in wb1 = load_workbook(path, read_only=true)
the method return a workbook with only the first sheet of the xlsx file

How can I convert Cell of Openpyxl from Text to Number format?

I wrote a code to convert a text file into excel file using Openpyxl extension of Python.
Although the value are setting properly into the column but they are showing as a text instead of number. Although I tried to convert, seems like it is not working.
Can anyone please correct the code?
import csv
import openpyxl
import openpyxl as oxl
input_file = r'C:\Python\Test.txt'
output_file = r'C:\Python\Test.xlsx'
wb = oxl.Workbook()
ws = wb.active
ws.number_format = 'General'
ws.title = "Waveform"
#ws = wb.create_sheet(title='Waveform')
with open(input_file, 'r') as data:
reader = csv.reader(data, delimiter='\t')
for row in reader:
ws.append(row)
for row in range(2, ws.max_row+1):
ws["{}{}".format("A", row)].number_format = 'General'
ws["{}{}".format("B", row)].number_format = 'General'
wb.save(output_file)
Here is the output excel file
the read data from txt file will be in string. So, as suggested by jezza, you need to convert list to float. You don't need the 'number_format` lines you have. Updated code is here. Note that the conversion map assumes all data can be converted to float (no text). The try/catch will basically skip the row if there is text on any row
import csv
#import openpyxl
import openpyxl as oxl
input_file = r'C:\Python\Test.txt'
output_file = r'C:\Python\Test.xlsx'
wb = oxl.Workbook()
ws = wb.active
#ws.number_format = 'General'
ws.title = "Waveform"
#ws = wb.create_sheet(title='Waveform')
with open(input_file, 'r') as data:
reader = csv.reader(data, delimiter='\t')
for row in reader:
try:
row = list(map(float, row))
ws.append(row)
except:
print("Skipping row ", row)
pass
#for row in range(2, ws.max_row+1):
# ws["{}{}".format("A", row)].number_format = 'General'
# ws["{}{}".format("B", row)].number_format = 'General'
wb.save(output_file)
Output

How to convert multiple CSV files to XLXS in Python?

I'm trying to convert all CSV files within a directory into one XLXS file with each csv file becoming a separate worksheet.
The code below works except when I provide the input path in this Line
"for filename in glob.glob(InputPath + "*.csv"):"
I get this error - InvalidWorksheetName: Invalid Excel character '[]:*?/' in sheetname
Does anyone have a suggestion how I can get around this? Full code is below - Thanks!
import xlsxwriter
import glob
import csv
InputPath = r"C:\\Users\\.spyder-py3\\"
workbook = xlsxwriter.Workbook(r"C:\\Users\\.spyder-py3\\Output\\compiled.xlsx")
for filename in glob.glob(InputPath + "\*.csv"):
ws = workbook.add_worksheet(str(filename.split('.')[0]))
spamReader = csv.reader(open(filename, 'r'), delimiter=',',quotechar='"')
row_count = 0
print(filename)
for row in spamReader:
for col in range(len(row)):
ws.write(row_count,col,row[col])
row_count +=1
workbook.close()
Try this:
import xlsxwriter
import glob
import csv
InputPath = r"C:\Users\.spyder-py3"
workbook = xlsxwriter.Workbook(r"C:\Users\.spyder-py3\Output\compiled.xlsx")
for filename in glob.glob(InputPath + r"\*.csv"):
ws = workbook.add_worksheet(str(filename.split('.')[0]))
spamReader = csv.reader(open(filename, 'r'), delimiter=',',quotechar='"')
row_count = 0
print(filename)
for row in spamReader:
for col in range(len(row)):
ws.write(row_count,col,row[col])
row_count +=1
workbook.close()

Getting wrong value when reading an xlsx file using openpyxl

I'm trying to read values from an xlsx file containing formulas using openpyxl; however, I noticed that for some cells, I'm getting a wrong value.
Here's the XLSX example:
Here's the result I get:
The code:
wb = openpyxl.load_workbook(excel_file, data_only=True)
# getting all sheets
sheets = wb.sheetnames
print(sheets)
# getting a particular sheet
worksheet = wb["Feuil1"]
print(worksheet)
# getting active sheet
active_sheet = wb.active
print(active_sheet)
# reading a cell
print(worksheet["A1"].value)
excel_data = list()
# iterating over the rows and
# getting value from each cell in row
for row in worksheet.iter_rows():
row_data = list()
for cell in row:
#cell.number_format='0.0########'
print(cell.number_format)
row_data.append(str(cell.value))
print(cell.value)
excel_data.append(row_data)
return render(request, 'myapp/index.html', {"excel_data":excel_data})
Hey What you want from an open excel file means which type of format do you gate
data,
This My answer for get data from excel file with xlrd.
import xlrd
from xlrd import open_workbook
fp = tempfile.NamedTemporaryFile(delete= False, suffix=filetype)
fp.write(binascii.a2b_base64(selected file))
workbook = xlrd.open_workbook(file name)
sheet = workbook.sheet_by_name(sheet name)
row = [c or '' for c in sheet.row_values(header_row)]
first_row = []
for col in range(sheet.ncols):
first_row.append(sheet.cell_value(0,col) )
archive_lines = []
for row in range(1, sheet.nrows):
elm = {}
for col in range(sheet.ncols):
elm[first_row[col]]=sheet.cell_value(row,col)
archive_lines.append(elm)

How to convert multiple xlsx sheet to csv using python

I can able to convert xlsx to csv in the case of single excel sheet.
How can i do the same in the case of multiple sheet in single excel file?
I have tried:
workBook = xlrd.open_workbook(filePath)
sheet_names = workBook.sheet_names()
lenth = len(sheet_names)
for i in range(0,lenth):
sheet = workBook.sheet_by_name(sheet_names[i])
yourcsvFile = open(csvPath, 'wb')
wr = csv.writer(yourcsvFile, quoting=csv.QUOTE_ALL)
for rownum in xrange(sheet.nrows):
wr.writerow(sheet.row_values(rownum))
yourcsvFile.close()
Try this
import sys
import xlrd
import csv
filePath = sys.argv[1] # user input file
csvPath = sys.argv[2]
workBook = xlrd.open_workbook(filePath)
sheet_names = workBook.sheet_names()
list_sheet = []
lenth = len(sheet_names)
for i in range(0,lenth):
sheet = workBook.sheet_by_name(sheet_names[i])
list_sheet.append(sheet)
yourcsvFile = open(csvPath, 'wb')
wr = csv.writer(yourcsvFile, quoting=csv.QUOTE_ALL)
total_row = list_sheet[0].ncols
for k in xrange(0,1):
for rownum in xrange(list_sheet[k].nrows):
wr.writerow(list_sheet[k].row_values(rownum))
if len(sheet_names) > 1:
for k in xrange(1,len(list_sheet)):
if list_sheet[k].ncols != total_row:
continue
for rownum in xrange(1,list_sheet[k].nrows):
wr.writerow(list_sheet[k].row_values(rownum))
yourcsvFile.close()

Categories