patelbg2001
asked on
Append Excel Worksheet Cell Range Format Using Python
Hi, I'm trying to open an Excel file in python, go to a worksheet and append the format from general to number or currency for a cell range, but get the following error -
File ".../python/html-table-parser/grapec ore1.py", line 227
incomesheet.append(cell_range, , money_format)
^
SyntaxError: invalid syntax
#Open workbook
from xlsxwriter.utility import xl_range
workbook = xlsxwriter.Workbook(xlfile)
#Select worksheet
incomesheet = workbook.get_worksheet_by_name(Income)
# Add a number format for cells with money.
money_format = workbook.add_format({'num_format': '$#,##0'})
#money_format = workbook.add_format({'num_format': '[Blue]£#,##0.00_);[Red](£#,##0.00)'})
cell_range = xl_range(2, 1, 28, 4)
incomesheet.append(cell_range, money_format)
#Close workbook
workbook.close()
ASKER
Thats how to error was presented, in Spyder. What is the method to append a range of cells?
Does this work for you?
incomesheet.write(cell_range,'', money_format)
ASKER
The following error -
How should the excel cell be formatted using python?
incomesheet.append(cell_range,'',mon ey_format)
AttributeError: 'NoneType' object has no attribute 'append'
How should the excel cell be formatted using python?
I used .write instead of .append, did you notice that?
I don't think xlsxwriter supports .append.
I don't think xlsxwriter supports .append.
ASKER
the error is similar
File ".../python/html-table-par ser/grapec ore1.py", line 229, in <module>
incomesheet.write(cell_ran ge,'', money_format)
AttributeError: 'NoneType' object has no attribute 'write'
File ".../python/html-table-par
incomesheet.write(cell_ran
AttributeError: 'NoneType' object has no attribute 'write'
That error means Sheet incomesheet is not defined that maybe because you didn't run the code from beginning.
ASKER
Using Python, I can create worksheet and populate the sheet with data.
I would like to update a specific cell range from 'General to 'Number'
The current code doesn't work, and I'm open to alternatives to achieve this.
I would like to update a specific cell range from 'General to 'Number'
The current code doesn't work, and I'm open to alternatives to achieve this.
Does this not work for you?
import xlsxwriter
from xlsxwriter.utility import xl_range
xlfile = "Test.xlsx"
workbook = xlsxwriter.Workbook(xlfile)
# Add a worksheet called 'Income'
incomesheet = workbook.add_worksheet('Income')
# Add a number format for cells with money.
money_format = workbook.add_format({'num_format': '$#,##0'})
cell_range = xl_range(2, 1, 28, 4)
incomesheet.write(cell_range,'', money_format)
#Close workbook
workbook.close()
ASKER
'xlsxwriter' overwrites existing data in the spreadsheet along with all other sheets, its only good, if importing new information.
Using openpyxl, I an update a cell, but I wanted to update a range like C4:E22. Any ideas?
Using openpyxl, I an update a cell, but I wanted to update a range like C4:E22. Any ideas?
wb = openpyxl.load_workbook(xlfile)
ws = wb['Income']
ncell = ws.cell('B2')
ncell.number_format = '0.00E+00'
#wb.save(xlfile)
One way is this...
import openpyxl
# Assuming File1.xlsx already exists
xlfile = 'File1.xlsx'
wb = openpyxl.load_workbook(xlfile)
# Assuming File1.xlsx contains a Sheet called Income
ws = wb['Income']
for row in range(4,23):
for col in range(2,6):
ws.cell(row,col).number_format = '0.00E+00'
wb.save(xlfile)
ASKER
Excel, doesn't like the cell update?
Wondering if this is faster done though vb?
Wondering if this is faster done though vb?
You started with xlsxwriter and then switched to openpyxl and now you are talking about vba. You should seriously make up your mind about how you want to deal with the excel file. :)
Since the original question you started with has been answered, I suggest you to close this question by accepting the answer and then open a new question with your new query.
Since the original question you started with has been answered, I suggest you to close this question by accepting the answer and then open a new question with your new query.
ASKER
#Open workbook
from openpyxl.styles import Alignment
wb = openpyxl.load_workbook(xlfile)
ws = wb['sheet2']
m_row = ws.max_row
# Loop will print all values
# of first column
for i in range(1, m_row + 1):
cell_obj = ws.cell(row = i, column = 1)
colA_alignment = Alignment(horizontal="left", vertical="center")
cell_obj.alignment = colA_alignment
for i in range(1, m_row + 1):
cell_obj = ws.cell(row = i, column = 2)
colB_alignment = Alignment(horizontal="center", vertical="center")
cell_obj.alignment = colB_alignment
for i in range(1, m_row + 1):
cell_obj = ws.cell(row = i, column = 3)
colC_alignment = Alignment(horizontal="left", vertical="center")
cell_obj.alignment = colC_alignment
for i in range(1, m_row + 1):
cell_obj = ws.cell(row = i, column = 4)
colD_alignment = Alignment(horizontal="left", vertical="center", wrapText=True)
cell_obj.alignment = colD_alignment
wb.save(xlfile)
from openpyxl.styles import Alignment
wb = openpyxl.load_workbook(xlfile)
ws = wb['sheet2']
wb.save(xlfile)
@Patel
Where do you stand with this question?
Where do you stand with this question?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window
and the second one (line 13 in the code snippet) has one commaOpen in new window