1 Star 5 Fork 8

1264644959 / blog

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
python读取word中表格.md 2.12 KB
一键复制 编辑 原始数据 按行查看 历史
1264644959 提交于 2020-09-18 13:08 . commit
import re
import uuid

import docx
import pymysql

dbParams = {
    "host" : "",
    "user":"root",
    "password":"",
    "db":"webpage_dev",
    "port":,
    "charset":"utf8"
}




conn = pymysql.Connect(**dbParams)
cursor = conn.cursor()
insert_num = 0
xiu_num= 0

db_key = 1
key = 3
path = "/tmp/pycharm_project_683/task_1/" + "重点实验室_docx" + '/'
filename = "30.docx"



doc = docx.Document(path + filename)
my_type = "重点实验室"



def to_mysql(basename):
    basename = basename
    baseid = uuid.uuid1()
    print(basename)
    global db_key
    if db_key:
        try:
            sql = "insert into sd_company(baseid,basename,type) value('{}','{}','{}')".format(baseid,
                                                                                              basename,
                                                                                              my_type)
            res = cursor.execute(sql)
            global insert_num
            insert_num += 1
            print(res)

        except Exception as e:
            print(e)
            conn.rollback()
        finally:
            conn.commit()


def chuli(basename):
    basename_copy = basename
    if "(" in basename:
        basename = basename.replace('(','(')
    if ")" in basename:
        basename = basename.replace(')',')')

    if "(" in basename:
        s_index = basename.index("(")
        e_index = basename.index(")")
        rm_str = basename[s_index:e_index+1]
        basename = basename.replace(rm_str,"")


    basename_new = re.sub("[A-Za-z0-9\!\%\[\]\,\,\.\。\n\、\\\\;]", "", basename)

    if basename_copy != basename_new:
        global xiu_num
        xiu_num += 1
        print('修改数据')
    basename = basename_new


    if basename:
        to_mysql(basename)

for table in doc.tables:
    print(len(table.rows))
    for row in table.rows:
        print('++++++++++++++++++++++++++++++++++')
        if len(row.cells) >= key:
            print(row.cells[key].text)
            chuli(row.cells[key].text)

print('共入库{}条数据'.format(insert_num))
print('共修改{}条数据'.format(xiu_num))
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/super__man/blog.git
git@gitee.com:super__man/blog.git
super__man
blog
blog
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891