python achieve replacement of particular content operation excel

This article describes the use python language, to achieve operating excel (xlsx) file, replace specific content needs to achieve by means of openyxl library.

Currently it implements three small functions:

1. Alternatively whole words (MODE1); (eg: whole words yocichen , alternatively be yoci X- Chen )

2. Alternatively matching portion of the character (MODE2); (eg: Thisis yoci Blog, alternatively be Thisis yocichen Blog)

3. filling whole words (MODE3); (eg: yoci , alternatively be yoci: A Foolish ), for adding characters after the character

Source:

 1 import openpyxl
 2 import re
 3 import traceback
 4 
 5 changeCells = 0
 6 
 7 # replace the special content
 8 """
 9 file: file path : str
10 mode: type of the operatoration : int
11 text: the string need to be replaceed : int or str
12 replaceText: replacement Text : int or str
13 """
14 def changeData(file, mode, text, replaceText):
15     # load the file(*.xlsx)
16     wb = openpyxl.load_workbook(file)
17     # ! deal with one sheet
18     ws = wb.worksheets[0]
19     global changeCells
20     # get rows and columns of file
21     rows = ws.max_row
22     cols = ws.max_column
23     changeFlag = False
24     try:
25         for row in range(1, rows+1):
26             for col in range(1, cols+1):
27                 content = ws.cell(row=row, column=col).value
28                 if(content != None):
29                     # mode1: fullmatch replacement
30                     if(mode == 1):
31                         if(content == text):
32                             ws.cell(row=row, column=col).value = replaceText
33                             changeFlag = True
34                             changeCells += 1
35                     # mode2: partial replacement
36                     elif(mode == 2):
37                         if(type(content) == str):
38                             ws.cell(row=row, column=col).value = content.replace(
39                                 text, replaceText, 1)
40                             changeFlag = True
41                             changeCells += 1
42                     # mode3: partialmatch and filling
43                     elif(mode == 3):
44                         if(type(content) == str):
45                             ws.cell(row=row, column=col).value = content.replace(
46                                 text, text+replaceText, 1)
47                             changeFlag = True
48                             changeCells += 1
49                     else:
50                         return 0
51         # status_1: modified success
52         if(changeFlag):
53             wb.save(file)
54             return changeCells
55         # status_2: no modified
56         else:
57             return changeCells
58     # status_3: exception
59     except Exception as e:
60         print(traceback.format_exc())
61 
62 
63 # read the content of file
64 """
65 file: file path : str
66 """
67 def rdxl(file):
68     # load the file(*.xlsx)
69     wb = openpyxl.load_workbook(file)
70     # ! deal with one sheet
71     ws = wb.worksheets[0]
72     global changeCells
73     # get rows and columns of file
74     rows = ws.max_row
75     cols = ws.max_column
76     changeFlag = False
77     cells = 0
78     for row in range(1, rows+1):
79         for col in range(1, cols+1):
80             content = ws.cell(row=row, column=col).value
81             print(content)
82             cells += 1
83     print('cells', cells)
84 
85 
86 if __name__ == "__main__":
87      res = changeData('D: \\ 001.xlsx ' ,. 1, 7777, ' bugs manufacturers ' )
 88       IF (RES =! None):
 89           Print ( ' modified ' , RES, ' at ' )
 90       # the else: 
91 is       #      Print ( 'operation failed: \ n-' + RES) 
92       rdxl ( " D: \\ 001.xlsx ' )

Guess you like

Origin www.cnblogs.com/yocichen/p/11693243.html