欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Python把word文档批量写入MySQL数据库

程序员文章站 2022-04-18 18:48:02
...

话不多说了,希望大家喜欢,我也是新手。

首先第一步 :电脑磁盘中的xls,xlsx,doc,docx文件找出来文件名,路径保存到另一个Excel文件:


BASE_DIR = Path(__file__).resolve().parent.parent
data = []
i = 0
e = 0
r = 0
t = 0


workbookXls = xlwt.Workbook(encoding="utf-8")
worksheetXls = workbookXls.add_sheet("xls")
workbookXlsx = xlwt.Workbook(encoding="utf-8")
worksheetXlsx = workbookXlsx.add_sheet("xlsx")
workbookDoc  = xlwt.Workbook(encoding="utf-8")
worksheetDoc = workbookDoc.add_sheet("doc")
workbookDocx = xlwt.Workbook(encoding="utf-8")
worksheetDocx = workbookDocx.add_sheet("a1")
for disk in psutil.disk_partitions():
    # 获取磁盘列表['C:\\', 'D:\\', 'E:\\', 'F:\\', 'G:\\', 'H:\\', 'I:\\', 'J:\\', 'K:\\', 'L:\\', 'M:\\']
    disk_list = disk.device
    for root, dirs, files in os.walk(disk_list):
        for a_files in files:
            suffex_list = [".xls", ".xlsx", ".doc", ".docx"]
            file_suffex_name = os.path.splitext(a_files)[1].lower()  # 获取文件后缀名 .xlsx等
            file_s_name_ = os.path.splitext(a_files)[0].lower()  # 获取文件名
            for a_suffex_list in suffex_list:
                if a_suffex_list == file_suffex_name and re.search("[\u4e00-\u9fa5]", file_s_name_) and not re.search(
                        "[\$|~]", root) and not re.search("[\$|~]", file_s_name_):
                    # data.append(os.path.join(root, a_files))
                    # print(file_suffex_name)
                    # print(a_files)
                    # print(root)
                    # print(os.path.join(root, a_files))
                    path = os.path.join(root, a_files)  # 获取整个文件地址与文件名
                    if file_suffex_name == ".xls":
                        i += 1
                    elif file_suffex_name == ".xlsx":
                        e += 1
                    elif file_suffex_name == ".doc":
                        r += 1
                    elif file_suffex_name == ".docx":
                        t += 1
                    #print(path)
    workbookXls.save("__$file_list_xls.xls")
    workbookXlsx.save("__$file_list_xlsx.xls")
    workbookDoc.save("__$file_list_doc1.xls")
    workbookDocx.save("__$file_list_docx.xls")





第二部,利用刚才保存过的文件路径和名称,读取doc,docx文件内容转换成HTML,然后写入MySQL数据库:



import time
from pydocx import PyDocX
import os
from win32com import client
import pythoncom
import xlrd
from pathlib import Path
import pymysql
import random


conn = pymysql.connect(host="127.0.0.1", user="root", password="root", db="mysite", port=3306)
BASE_DIR = Path(__file__).resolve().parent.parent

xls_path = "__$file_list_doc1.xls"
x_excel = xlrd.open_workbook(xls_path)
table = x_excel.sheet_by_index(0)
table_length = table.nrows  # 返回列的长度
indexnum =29648

for num in range(int(indexnum), int(table_length)):
    try:
        excel_file_path = table.cell(num, 1).value
        excel_file_root = str(table.cell(num, 2).value)
        excel_file_name = str(table.cell(num, 3).value)
        print(excel_file_path)
        as_docx_path = os.path.join(BASE_DIR, 'temp',
                                    "{}_{}.docx".format(time.strftime("%Y%m%d%H%M%S", time.localtime()), random.random()))

        as_docx_path_html = os.path.join(BASE_DIR, 'temp',
                                         "{}.html".format(
                                             time.strftime("%Y%m%d%H%M", time.localtime())))
        if os.path.splitext(excel_file_name)[1] == ".doc":
            pythoncom.CoInitialize()
            word = client.Dispatch("Word.Application")
            doc = word.Documents.Open(excel_file_path)
            doc.SaveAs(as_docx_path, 12, False, '', False, '', '', False, False, False, False)
            doc.Close()
            html = PyDocX.to_html(as_docx_path)
            f = open(as_docx_path_html, "w", encoding="utf-8")
            f.write(html)
            f.close()
            # return HttpResponse(as_docx_path_html)

        else:
            html = PyDocX.to_html(excel_file_path)
            f = open(as_docx_path_html, "w", encoding="utf-8")
            f.write(html)
            f.close()
            # return HttpResponse(as_docx_path_html)
        f = open(as_docx_path_html, 'r', encoding='utf-8')
        f = f.read()
        cursor = conn.cursor()
        table_name = "docx"
        sql_str_extis = "SHOW TABLES LIKE '{}'".format(table_name)
        if cursor.execute(sql_str_extis) == 0:
            sql_str = "CREATE TABLE `{}` (`id` int(99) NOT NULL AUTO_INCREMENT,`username` text DEFAULT NULL,`filename` text DEFAULT NULL,`pinyin` text DEFAULT NULL,`time` text DEFAULT NULL,`text` longtext,PRIMARY KEY (`id`)) ENGINE=MyISAM DEFAULT CHARSET=utf8;".format(
                table_name)
            cursor.execute(sql_str)
        insert_time = time.strftime("%Y%m%d%H%M", time.localtime())
        r = cursor.execute("INSERT INTO `{}` (`username`, `filename`, `pinyin`, `time`, `text`) VALUES ('adminstator', '{}', '_  ', '{}', '{}')".format(
                table_name, excel_file_name, insert_time, f))

        d = {"file": excel_file_path, "sql_response": r}
        print(d)
    except:
        continue


还有剩下的是Excel内容一行一行的写入数据库比较麻烦,所有下次给大家分享。。

相关标签: Python python