欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python讀取word文檔,插入mysql數(shù)據(jù)庫的示例代碼

 更新時間:2018年11月07日 09:39:17   作者:無所住心  
今天小編就為大家分享一篇python讀取word文檔,插入mysql數(shù)據(jù)庫的示例代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

表格內(nèi)容如下:

python讀取word,插入mysql

1、實現(xiàn)批量導(dǎo)入word文檔,取文檔標(biāo)題中的數(shù)字作為編號

2、除取上面打鉤的內(nèi)容需要匹配出來入庫入庫,其他內(nèi)容全部直接入庫mysql

# wuyanfeng
# -*- coding:utf-8 -*-
# 讀取docx中的文本代碼示例
import docx
import pymysql
import re
import os

# 創(chuàng)建數(shù)據(jù)庫鏈接
conn = pymysql.connect(
 host='rm-bp1vu5d84dg12c6d59o.mysql.rds.aliyuncs.com',
 port=3306,
 user='root',
 passwd='wYf092415*',
 db='pays',
 charset='utf8',
)
# 創(chuàng)建游標(biāo)
cursor = conn.cursor()

#切片函數(shù)
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(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("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::::::::::::::::::::", type(file))
 # file1 = file
 # file1 = str(file1)
 ddd = re.findall("知識庫\\\(\d+)", file)
 print("ddd:::::::::::", ddd)
 print("ddd[0]:::", ddd[0])
 ddd = int(ddd[0])
 print("ddd::::", type(ddd))

 file = docx.Document(file)
 # 讀取表格:
 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]
 '''低級處理方式
 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 = '法定職責(zé)'
  print("a:%s,b=%s" % (a, b), task_type)
 elif (a == 2) | (b == 2):
  print("333333333333333333333")
  task_type = '法定職責(zé),工作職責(zé) '
  print("a:%s,b=%s" % (a, b), task_type)
 '''
 #調(diào)用切片函數(shù)
 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

 # 獲取文檔對象
 # file = docx.Document("D:\\配置庫\\公案APP\\1.2 系統(tǒng)規(guī)格\\知識庫\\14人員死亡先期處置.docx")
 print("段落數(shù):" + str(len(file.paragraphs))) # 段落數(shù)為13,每個回車隔離一段
 lenn = len(file.paragraphs)
 print("len:", lenn)
 # 輸出每一段的內(nèi)容
 for para in file.paragraphs:
  print(para.text)

 # 輸出段落編號及段落內(nèi)容
 for i in range(len(file.paragraphs)):
  print("第" + str(i) + "段的內(nèi)容是:" + 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))
   '''低級處理方式
   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)
  '''
   #調(diào)用切片函數(shù)
   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('填表單位:(.*?)$', 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('文件: %s' % tmp_path)
   insettable(tmp_path)
  else:
   print('文件夾:%s' % tmp_path)
   traverse(tmp_path)


path = 'D:\\配置庫\公案APP\\1.2 系統(tǒng)規(guī)格\\知識庫'
traverse(path)

#單文件調(diào)測
# path = 'D:\\配置庫\\公案APP\\1.2 系統(tǒng)規(guī)格\\知識庫\\14人員死亡先期處置.docx'
# insettable(path)

# 關(guān)閉游標(biāo)
cursor.close()
# 關(guān)閉連接
conn.close()

以上這篇python讀取word文檔,插入mysql數(shù)據(jù)庫的示例代碼就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • pyhanlp安裝介紹和簡單應(yīng)用

    pyhanlp安裝介紹和簡單應(yīng)用

    這篇文章主要介紹了pyhanlp安裝介紹和簡單應(yīng)用,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-02-02
  • python數(shù)據(jù)化運營的重要意義

    python數(shù)據(jù)化運營的重要意義

    在本篇文章里小編給大家分享的是關(guān)于python數(shù)據(jù)化運營的重要意義,有興趣的朋友們可以學(xué)習(xí)下。
    2019-11-11
  • Python中assert函數(shù)的使用(含源代碼)

    Python中assert函數(shù)的使用(含源代碼)

    本文主要介紹了Python中assert函數(shù)的使用(含源代碼),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • Python實現(xiàn)動態(tài)圖解析、合成與倒放

    Python實現(xiàn)動態(tài)圖解析、合成與倒放

    這篇文章主要為大家詳細介紹了Python實現(xiàn)動態(tài)圖的解析、合成與倒放,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • Python 字符串中的字符倒轉(zhuǎn)

    Python 字符串中的字符倒轉(zhuǎn)

    實現(xiàn)字符倒轉(zhuǎn)的python函數(shù)
    2008-09-09
  • Python繪制3D圖形

    Python繪制3D圖形

    這篇文章主要介紹了Python繪制3D圖形,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值
    2018-05-05
  • 淺析Python3中的對象垃圾收集機制

    淺析Python3中的對象垃圾收集機制

    在Python中,它在每個對象中保持了一個計數(shù)器,用于記錄指向該對象的的引用的個數(shù)。這篇文章主要介紹了Python3中的對象垃圾收集機制,需要的朋友可以參考下
    2019-06-06
  • NumPy進行統(tǒng)計分析

    NumPy進行統(tǒng)計分析

    本文主要介紹了NumPy進行統(tǒng)計分析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05
  • python使用pygame實現(xiàn)笑臉乒乓球彈珠球游戲

    python使用pygame實現(xiàn)笑臉乒乓球彈珠球游戲

    這篇文章主要為大家詳細介紹了python使用pygame實現(xiàn)笑臉乒乓球彈珠球游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • python小白練習(xí)題之條件控制與循環(huán)控制

    python小白練習(xí)題之條件控制與循環(huán)控制

    Python 中的條件控制和循環(huán)語句都非常簡單,也非常容易理解,與其他編程語言類似,下面這篇文章主要給大家介紹了關(guān)于python小白練習(xí)題之條件控制與循環(huán)控制的相關(guān)資料,需要的朋友可以參考下
    2021-10-10

最新評論