pythonでwordの表を読み込む
4381 ワード
import docx
import pymysql
import os
import re
def scan_files(directory, prefix=None, postfix=None):
files_list = []
for root, sub_dirs, files in os.walk(directory):
for special_file in files:
if postfix:
if special_file.endswith(postfix):
files_list.append(os.path.join(root, special_file))
elif prefix:
if special_file.startswith(prefix):
files_list.append(os.path.join(root, special_file))
else:
files_list.append(os.path.join(root, special_file))
return files_list
def read_word():
#
db = pymysql.connect(host="192.168.0.22", user="public",
password="123456", database="zhaoyangyongxu", port=3307, charset='utf8')
# cursor()
cursor = db.cursor()
path = r'\\192.168.0.22\stock\ \50 '
file_paths = scan_files(directory=path)
p = re.compile(r'\d+')
for file_path in file_paths:
file = docx.Document(file_path)
# print(len(file.paragraphs))
# for para in file.paragraphs:
# print(para.table)
file_name = os.path.split(file_path)[-1]
date_time = p.findall(file_name)[1]
tables = file.tables
table = tables[0]
try:
sql_delete = "DELETE FROM zhaoyang WHERE datetime = '%s'" % (date_time)
cursor.execute(sql_delete)
db.commit()
except:
print('cuowu')
for i in range(1, len(table.rows)): #
result = table.cell(i, 0).text + " ---" + table.cell(i,
1).text + "-----------" + table.cell(i, 2).text
sql_insert = r'insert into zhaoyang values("%s","%s","%s","%s")' % (
date_time, table.cell(i, 0).text, table.cell(i, 1).text, table.cell(i, 2).text)
# execute SQL
cursor.execute(sql_insert)
db.commit()
print('success ...{0}'.format(result),date_time)
db.close()
if __name__ == '__main__':
read_word()