The table reads as follows:
1, implement batch import Word document, take the number of the document title as number
2, in addition to the above to check the contents of the hook should be matched out of the storage, the other contents of all direct storage MySQL
# Wuyanfeng
#-*-Coding:utf-8-*-
# Read the text code example in docx
Import docx
Import Pymysql
Import re
Import OS
# Create a database link
conn = Pymysql.connect (
Host= ' rm-bp1vu5d84dg12c6d59o.mysql.rds.aliyuncs.com ',
port=3306,
User= ' Root ',
Passwd= ' wyf092415* ',
Db= ' pays ',
charset= ' UTF8 ',
)
# Create Cursors
cursor = Conn.cursor ()
#切片函数
def section (INFO,KEY,LEN11):
A = Len (info)
Print (A, type (a))
d = []
E = 0
g =-1
i = 0
Task_class=[]
While I < Len (info):
# for I in range (len (info)):
# i+=1
Print ("I::::", i)
Try
#c = Info.index ("A", e)
#print ("C:::;:", C)
c = Info.index (Key, E)
#print ("C:::;:", C)
Print ("C-type judgment", type (c))
Except ValueError:
Print (ValueError)
Try
if (c! = ") & (G < int (c)):
D.append (c)
g = C
i = C + 1
Print ("ILLLLDDDD:", i)
E = c + 1
Continue
Elif (c = = "):
Break
Except Unboundlocalerror:
Print (Unboundlocalerror)
Return Task_class
Break
Print ("D", D, type (d))
Print (D[0], type (d[0]))
Print (Length of "D:", Len (d))
#开始切片
If Len (d)! = 0:
For j in Range (Len (d)):
Print ("Info11::", info, type (info))
info = ". Join (Info)
Print ("info222:::", Info,type (info))
Print ("d[%d]"%j,d[j])
#print ("D[j]:5"%j,info[d[j]:5])
Llll = info[d[j]+1:d[j]+5]
Print ("D[%d]:5"% J, llll)
Task_class.append (LLLL)
Print ("Task_class::11", Task_class)
Task_class= ",". Join (Task_class)
Print ("str1112222", Task_class)
Return Task_class
def insettable (file):
Print ("File:::::::::::::,") ("file")
Print ("Type:::::::::::::," ",")------
# file1 = File
# file1 = str (file1)
DDD = Re.findall ("Knowledge Base \\\ (\d+)", file)
Print ("DDD.::::::::::", DDD)
Print ("ddd[0]:::", ddd[0])
DDD = Int (ddd[0])
Print ("DDD::::", type (DDD))
File = docx. Document (file)
# Read the table:
t = file.tables[0]
Print (t)
Print ("1:", T.cell (0, 0). Text) # 1
Cell1 = T.cell (0, 0). Text
Print ("Tyep:::", type (T.cell (0, 0). Text))
Print ("2:", T.cell (0, 1). Text) # 2
Cell2 = T.cell (0, 1). Text
Print ("2:", T.cell (0, 2). Text) # 2
Cell3 = T.cell (0, 2). Text
Print ("2:", T.cell (0, 3). Text) # 2
CELL4 = T.cell (0, 3). Text
Print ("CELL4:::::::::", CELL4)
Print ("3:", T.cell (1, 0). Text) # 3
CELL5 = T.cell (1, 0). Text
Print ("4:", T.cell (1, 2). Text) # 4
CELL6 = T.cell (1, 2). Text
Print ("5:", T.cell (1, 3). Text) # 5
Task_type = T.cell (1, 3). Text
# Task_type = Re.findall ('. *[?√] (. *) $ ', CELL7)
# Task_type = '. Join (CELL7)
Print ("task_type111111:", Task_type)
# Task_class = Task_class[0:4]
"' low-level processing method
a = Int (Task_type.count ("?"))
Print ("a| | | | | | |", a, type (a))
b = Int (Task_type.count ("√"))
Print ("b| | | | | | |", B, type (a))
if (a = = 1) | (b = = 1):
Print ("111111111111111111")
# Task_type = Re.findall ('. *[?√] (. *) $ ', task_type)
Task_type = Re.findall ('. *[?√] (. *) $ ', task_type)
Print ("Task_type1", Task_type)
Task_type = ". Join (Task_type)
Print ("Task_type2", Task_type)
Task_type = Task_type[0:4]
Print ("task_type3:d:%s,b=%d"% (A, b), Task_type)
elif (A = = 0) & (b = = 0):
Print ("2222222222222222222")
Task_type = ' statutory duties '
Print ("a:%s,b=%s"% (A, b), Task_type)
elif (A = = 2) | (b = = 2):
Print ("333333333333333333333")
Task_type = ' statutory duties, Job responsibilities '
Print ("a:%s,b=%s"% (A, b), Task_type)
‘‘‘
#调用切片函数
task_type1 = section (Task_type, "√", 4)
task_type2 = section (Task_type, "?", 4)
Task_type1 = "". Join (TASK_TYPE1)
Task_type2 = "". Join (TASK_TYPE2)
Print ("Task_type1:::", Task_type1,type (Task_type1))
Print ("Task_type2:::", Task_type2,type (task_type2))
If Task_type1.strip ()! = "":
Task_type = Task_type1
Print ("task_type111:::", Task_type1)
Elif Task_type2.strip ()! = "":
Task_type = Task_type2
Print ("task_type222:::", Task_type2)
Print ("6:", T.cell (1, 4). Text) # 6
Cell8 = T.cell (1, 4). Text
Print ("7:", T.cell (2, 1). Text) # 7
Cell9 = T.cell (2, 1). Text
# Get Document Object
# file = docx. Document ("d:\\ configuration library \ \ app\\1.2 system specifications \ \ Knowledge Base \\14 personnel death advance disposition." Docx ")
Print ("Number of paragraphs:" + str (len (file.paragraphs)) # Number of paragraphs is 13, each carriage return is isolated for a period
Lenn = Len (file.paragraphs)
Print ("Len:", Lenn)
# Output the contents of each paragraph
For para in file.paragraphs:
Print (Para.text)
# Output Paragraph number and paragraph content
For I in range (len (file.paragraphs)):
Print ("s" + str (i) + "section content is:" + file.paragraphs[i].text)
LIST6 = []
For I in range (len (file.paragraphs)):
If 0 = = I:
Print ("I:", i)
LIS0 = File.paragraphs[i].text
Print ("List0:", LIS0)
Print (Type (LIS0))
Elif 1 = = I:
Print ("I:", i)
Task_class = File.paragraphs[i].text
Print ("Lis1", Task_class,type (Task_class))
"' low-level processing method
Print ("task_class111111:", Task_class)
c = Int (Task_class.count ("?"))
Task_class = ". Join (Task_class)
#print (Task_class.index ('? '))
Print ("c| | | | | | |", C, type (c))
d = Int (Task_class.count ("√"))
Print (Task_class.index (' √ '))
Print ("d| | | | | | |", D, type (d))
Task_class = Re.findall (R ' [? √] (?:. *) ', task_class)
Task_class = ". Join (Task_class)
Task_class = Task_class[1:5]
Print ("Task_class", Task_class)
‘‘‘
#调用切片函数
Task_class1 = section (Task_class, "√", 4)
Task_class2 = section (Task_class, "?", 4)
Task_class1 = "". Join (TASK_CLASS1)
Task_class2 = "". Join (TASK_CLASS2)
Print ("Task_class1:::", Task_class1,type (TASK_CLASS1))
Print ("Task_class2:::", Task_class2,type (TASK_CLASS2))
If Task_class1.strip ()! = "":
Task_class = Task_class1
Print ("TASK_CLASS11:::", Task_class1)
Elif Task_class2.strip ()! = "":
Task_class = Task_class2
Print ("Task_class22:::", Task_class2)
If 2 = = I:
Print ("I:", i)
Lis2 = File.paragraphs[i].text
Print ("Lis2", Lis2)
Print (Type (LIS2))
preparer = Re.findall (' Filling unit: (. *?) $ ', Lis2)
preparer = ". Join (Preparer)
Print ("preparer:%s"% preparer)
# elif 3 = = I:
# Print ("I:", i)
# LIS3 = File.paragraphs[i].text
Elif 3 = = I:
Print ("I:", i)
LIS4 = File.paragraphs[i].text
Print ("Lis4", LIS4)
Print (Type (LIS4))
Elif 3 < i < lenn-1:
Print ("I:", i)
Print (File.paragraphs[i].text)
Print (Type (file.paragraphs[i].text))
# List6[i-5] = List6.append (file.paragraphs[i].text)
List6.append (str (file.paragraphs[i].text). Strip (' \xa0 '))
# list6.append ("%s\n"% str (file.paragraphs[i].text). Strip (' \xa0 '))
Print (LIST6)
Key_steps = "\ n". Join (LIST6)
# print ("key_steps:\n", Key_steps.strip (' \ n '))
Cursor.execute (
Insert INTO ' t_knowledge_base ' (' No ', ' preparer ', ' task_class ', ' task_name ', ' task_specification ', ' task_type ', ' task_ ' Desc ', ' task_basis ', ' key_steps ') VALUES ('%d ', '%s ', '%s ', '%s ', '%s ', '%s ', '%s ', NULL, '%s ') "% (
DDD, preparer, Task_class, Cell2, Cell4, Task_type, Cell9, Key_steps))
Conn.commit ()
def traverse (f):
FS = Os.listdir (f)
For F1 in FS:
Tmp_path = Os.path.join (f, F1)
If not Os.path.isdir (Tmp_path):
Print (' File:%s '% Tmp_path)
Insettable (Tmp_path)
Else
Print (' folder:%s '% Tmp_path)
Traverse (Tmp_path)
Path = ' d:\\ configuration library \ app\\1.2 System specifications \ \ Knowledge Base '
Traverse (PATH)
#单文件调测
# path = ' d:\\ configuration library \ \ app\\1.2 system specifications \ \ Knowledge Base \\14 personnel died in advance. docx '
# insettable (PATH)
# Close Cursors
Cursor.close ()
# Close Connection
Conn.close ()
Python reads a Word document and inserts a MySQL DB instance