-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopyexcelformatting.py
More file actions
90 lines (55 loc) · 2.96 KB
/
copyexcelformatting.py
File metadata and controls
90 lines (55 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env python
# coding: utf-8
# In[1]:
from copy import copy
import openpyxl
import os
#this program is used to copy the formatting of a column, row or entire sheet
def copy_cell_format(from_cell, to_cell):
# this function copies a cell's format to antother cell
if from_cell.has_style:
to_cell.font = copy(from_cell.font)
to_cell.border = copy(from_cell.border)
to_cell.fill = copy(from_cell.fill)
to_cell.number_format = copy(from_cell.number_format)
to_cell.protection = copy(from_cell.protection)
to_cell.alignment = copy(from_cell.alignment)
def copy_column_format(in_sheet, out_sheet = None, from_column_idx = None, to_column_idx = None):
# this function copies the format from a column of a sheet to another column
if from_column_idx == None:
# the default is to the use the last row of the table
from_column_idx = in_sheet.max_column
if to_column_idx == None:
# the default is for the column format to be appended to the right of the table
to_column_idx = from_column_idx + 1
if out_sheet == None:
# the default is for the out_sheet to be the same as the in_sheet
out_sheet = in_sheet
for each_row in list(range(1, in_sheet.max_row)):
from_cell = in_sheet.cell(row = each_row, column = from_column_idx)
to_cell = out_sheet.cell(row = each_row, column = to_column_idx)
if from_cell.has_style:
copy_cell_format(from_cell, to_cell)
def copy_row_format(in_sheet, out_sheet = None, from_row_idx = None, to_row_idx = None):
# this function copies the format from a row of a sheet to another row
if from_row_idx == None:
# the default is to the use the last row of the table
from_row = in_sheet.max_row
if to_row_idx == None:
# the default is for the column format to be appended to the bottom of the table
to_row_idx = from_row + 1
if out_sheet == None:
# the default is for the out_sheet to be the same as the in_sheet
out_sheet = in_sheet
for each_column in list(range(1, in_sheet.max_column)):
from_cell = in_sheet.cell(row = from_row_idx, column = each_column)
to_cell = out_sheet.cell(row = to_row_idx, column = each_column)
if from_cell.has_style:
copy_cell_format(from_cell, to_cell)
def copy_formatting(from_sheet, to_sheet):
# this may not work on styles that are defined in the workbook, but alas, it's something
# another function should be created to examine all of the styles in the workbook and/or sheet
row_idx = 1
for row in from_sheet.iter_rows():
copy_row_format(from_sheet, to_sheet, row_idx, row_idx)
row_idx += 1