35
loading...
This website collects cookies to deliver better user experience
pip install openpyxl
from openpyxl import load_workbook, workbook
from reviewer import reviewer_geo
input_file_path = 'Geo Demo.xlsx'
workbook = load_workbook(input_file_path)
worksheet = workbook['Sheet1']
col_latitude = 'B'
col_longitude = 'C'
Sheet1
from Geo Demo.xlsx. You can also use worksheet = workbook[workbook.sheetnames[0]]
to load the first sheet from Geo Demo.xlsx, this is very useful because user may changes the default sheet name.review_geo
function, and finally save a new excel file with reviewed data. Add the following line to geo.py:row_start = 2 # geo data starts at row 2
row_end = 10 # geo data ends at row 10
for i in range(row_start, row_end+1):
cell_site = worksheet[f'{col_site}{i}']
cell_latitude = worksheet[f'{col_latitude}{i}']
cell_longitude = worksheet[f'{col_longitude}{i}']
reviewer_geo(cell_latitude)
reviewer_geo(cell_longitude)
# save the updated excel
output_file_path = 'Geo Demo Reviewed.xlsx'
workbook.save(output_file_path)
write_comment
can add red background and comment to a cell. add_green_bg
simply add a green background to a cell.from openpyxl.comments import Comment
from openpyxl.styles import PatternFill
def write_comment(cell, comment_text):
red_bg = PatternFill(fill_type='solid',
start_color='FF0000',
end_color='FF0000')
cell.fill = red_bg
comment = Comment(comment_text, 'Python')
cell.comment = comment
def add_green_bg(cell):
green_bg = PatternFill(fill_type='solid',
start_color='00FF00',
end_color='00FF00')
cell.fill = green_bg
def reviewer_geo(cell):
val_geo = cell.value
str_geo = str(val_geo)
# check if cell is empty
if val_geo is None:
write_comment('This cell must be filled')
# if cell is not empty, check if cell contains float
elif isinstance(val_geo, int):
write_comment('This cell must be a float number')
else:
# if cell is not empty, but contains characters other than ditits and dicimal point
for c in str_geo:
if not c.isdigit() and c != '.':
write_comment('This cell must be a float number')
return
# if cell value is a float number, check if cell contains float with 6 decimal places
len_geo = len(str_geo.split('.')[-1])
if len_geo != 6:
if len_geo > 6:
cell.value = "{:.6f}".format(float(str_geo)) # remove extra decimal points
else:
write_comment('Geo data must be a float number with 6 decimal places')
return