from openpyxl import load_workbook
# Set the filename to avoid issues with blanks, etc.
file = input("Enter file name: ")
wb = load_workbook(file)
ws = wb.active
ws.title = "Activities"
# Set properties for styling the header cells
from openpyxl.styles import Alignment, Font, PatternFill, Border, Side, NumberFormatDescriptor
# Define a Font object ft where text is bold (b=True)
ft = Font(b=True)
# Define an Alignment object al where contents of cell are centered horizontally and vertically
al = Alignment(horizontal='center', vertical='center', wrapText=True)
# Define fill color for header
fl = PatternFill(patternType='solid', fgColor = 'DDEBF7')
# Define cell borders for header
bd = Border(left=Side(border_style='thin', color='000000'),
right=Side(border_style='thin', color='000000'),
top=Side(border_style='thin', color='000000'),
bottom=Side(border_style='thin', color='000000'),
diagonal=Side(border_style='thin', color='000000'),
vertical=Side(border_style='thin', color='000000'),
horizontal=Side(border_style='thin', color='000000'),
outline=True)
# Iterate thru the columns in the header row and assign font, alignment, fill and border attributes
for row in ws['A1':'K1']:
for cell in row:
cell.font = ft
cell.alignment = al
cell.fill = fl
cell.border = bd
# Change heading text
# ws['B1'].value = "zCA Manager"
# Set height of header row
# ws.row_dimensions[1].height = 32 # Set header row to double height to accomodate word wrap
# Adjust column widths and hide appropriate columns
# ws.column_dimensions['A'].hidden = True
ws.column_dimensions['A'].width = 52
ws.column_dimensions['B'].width = 29
ws.column_dimensions['B'].hidden = True
ws.column_dimensions['C'].width = 29
ws.column_dimensions['C'].hidden = True
ws.column_dimensions['D'].width = 29
ws.column_dimensions['E'].width = 23
ws.column_dimensions['F'].width = 61
ws.column_dimensions['G'].width = 25
ws.column_dimensions['I'].width = 17
ws.column_dimensions['J'].width = 9.67
ws.column_dimensions['L'].width = 9.67
# iterate through rows and adjust values of certain cells
# Define fill color for workshop rows
fl_wkshp = PatternFill(patternType='solid', fgColor = 'D7FBE0')
for row in range(2, ws.max_row + 1):
rowNum = str(row)
# Trim the time off the end of the date fields
if not (ws['J' + rowNum].value == None):
ws['J' + rowNum].value = ws['J' + rowNum].value[:10] # Trim the time off of the date/time field generated by SC
if not (ws['K' + rowNum].value == None):
ws['K' + rowNum].value = ws['K' + rowNum].value[:10] # Trim the time off of the date/time field generated by SC
if not (ws['L' + rowNum].value == None):
ws['L' + rowNum].value = ws['L' + rowNum].value[:10] # Trim the time off of the date/time field generated by SC
# Populate the Client Name field
if not (ws['B' + rowNum].value == None):
ws['D' + rowNum].value = ws['B' + rowNum].value
elif not (ws['C' + rowNum].value == None):
ws['D' + rowNum].value = ws['C' + rowNum].value
else:
ws['D' + rowNum].value = None
# Look for workshop entries and highlight the rows if the word appears there
print ("Column A Value: %s" % ws['A' + rowNum].value)
if ("workshop" in ws['A' + rowNum].value):
for cell in row:
cell.fill = fl_wkshp
wb.save(file)BillMBP15:bin Bill$ pip3.5 install --pre openpyxl
Requirement already satisfied (use --upgrade to upgrade): openpyxl in /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages
Requirement already satisfied (use --upgrade to upgrade): jdcal in /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages (from openpyxl)
Requirement already satisfied (use --upgrade to upgrade): et_xmlfile in /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages (from openpyxl)
BillMBP15:bin Bill$ pip3.5 show openpyxl
---
Metadata-Version: 1.1
Name: openpyxl
Version: 2.3.3
Summary: A Python library to read/write Excel 2010 xlsx/xlsm files
Home-page: http://openpyxl.readthedocs.org
Author: See AUTHORS
Author-email: eric.gazoni@gmail.com
License: MIT/Expat
Location: /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages
Requires: jdcal, et-xmlfile
Classifiers:
Development Status :: 5 - Production/Stable
Operating System :: MacOS :: MacOS X
Operating System :: Microsoft :: Windows
Operating System :: POSIX
License :: OSI Approved :: MIT License
Programming Language :: Python
Programming Language :: Python :: 2.6
Programming Language :: Python :: 2.7
Programming Language :: Python :: 3.3
Programming Language :: Python :: 3.4So I'm running code like this:
for row in range(2, ws.max_row + 1):
rowNum = str(row)
# Trim the time off the end of the date fields
if not (ws['J' + rowNum].value == None):
ws['J' + rowNum].value = ws['J' + rowNum].value[:10] # Trim the time off of the date/time field generated by SC
if not (ws['K' + rowNum].value == None):
ws['K' + rowNum].value = ws['K' + rowNum].value[:10] # Trim the time off of the date/time field generated by SC
if not (ws['L' + rowNum].value == None):
ws['L' + rowNum].value = ws['L' + rowNum].value[:10] # Trim the time off of the date/time field generated by SC
# Populate the Client Name field
if not (ws['B' + rowNum].value == None):
ws['D' + rowNum].value = ws['B' + rowNum].value
elif not (ws['C' + rowNum].value == None):
ws['D' + rowNum].value = ws['C' + rowNum].value
else:
ws['D' + rowNum].value = None
# Look for workshop entries and highlight the rows if the word appears there
print ("Column A Value: %s" % ws['A' + rowNum].value)
if ("workshop" in ws['A' + rowNum].value):
for cell in row:
cell.fill = fl_wkshp