Link to home
Start Free TrialLog in
Avatar of patelbg2001
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/grapecore1.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()

Open in new window

Avatar of aikimark
aikimark
Flag of United States of America image

You have shown us two different statements.  The first one (in your question text) has two commas
incomesheet.append(cell_range, , money_format)

Open in new window

and the second one (line 13 in the code snippet) has one comma
incomesheet.append(cell_range, money_format)

Open in new window

Avatar of patelbg2001
patelbg2001

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)

Open in new window

The following error -

incomesheet.append(cell_range,'',money_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.
the error is similar


  File ".../python/html-table-parser/grapecore1.py", line 229, in <module>
    incomesheet.write(cell_range,'', money_format)

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.
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.
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()

Open in new window

'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?

wb = openpyxl.load_workbook(xlfile)
ws = wb['Income']
ncell = ws.cell('B2')
ncell.number_format = '0.00E+00'
#wb.save(xlfile)

Open in new window

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)

Open in new window

Excel, doesn't like the cell update?
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.
#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)

Open in new window

@Patel

Where do you stand with this question?
ASKER CERTIFIED SOLUTION
Avatar of patelbg2001
patelbg2001

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial