[英]Python program to read Excel files and copy their sqls into new text file
I am very new to Python, need some help. 我对Python很陌生,需要一些帮助。
I am using Python 3.6.1. 我正在使用Python 3.6.1。 I am working on creating a Python script which will read all the Excel (.xlsx) files from a folder.
我正在创建一个Python脚本,该脚本将从文件夹中读取所有Excel(.xlsx)文件。 In those files, it should search for files which contain a SQL tab as worksheet and then it should copy the A2 column from the SQL tab of the worksheet and create a .sql with same name as the Excel file appended to it.
在这些文件中,它应搜索包含“ SQL”选项卡作为工作表的文件,然后应从工作表的“ SQL”选项卡复制A2列,并创建一个与附加到其上的Excel文件同名的.sql。 Till now I am able to read all the .xlsx files from the folder, look if they have a SQL tab and then copy the A2 column.
到目前为止,我已经能够从该文件夹中读取所有.xlsx文件,查看它们是否具有SQL选项卡,然后复制A2列。 Can someone please help me in guiding on how I can create a .sql file and copy the sql of all the Excel files to these .sql files with the Excel name appended to the .sql files.
有人可以帮助我指导如何创建.sql文件,并将所有Excel文件的sql复制到这些.sql文件中,并将Excel名称附加到.sql文件中。
Following is where I am right now, 以下是我现在的位置
import os
import pandas as pd
from os import walk
from openpyxl import load_workbook
cpt = sum([len(files) for r, d, files in os.walk(r"C:\Users\Data_Dumps\New folder")])
for dirpath, dirnames, filenames in walk(r'C:\Users\Data_Dumps\New folder'):
print('Total Files in the folder are: %d' %cpt)
print('Total filenames are')
print(filenames)
files = os.listdir(dirpath)
print(files)
files_xls = [f for f in filenames if f[-4:] == 'xlsx']
print('Excel files are:')
print(files_xls)
#Creating data frame to store SQL
df = pd.DataFrame()
files_xls_string = '\n'.join(files_xls)
df = df.append(files_xls_string)
wb = load_workbook(files_xls_string, read_only=True)
if 'SQL' in wb.sheetnames:
sheet = wb.get_sheet_by_name('SQL')
SQL = sheet['A2'].value
Note to @Shams : This is a SQL SELECT statement, where are your data ?
@Shams注意 :这是一条SQL SELECT语句,您的数据在哪里?
A2 value eg:A2值,例如:
select ip.item_code , ip.gla_code_ar ,r.ruleit_text LCSPAINTSZ from item i , itemplant ip , ruleit r where i.item_code = ip.item_code and ip.plant_code = '14' and i.item_active = 1 --and i.item_code = '457332' and i.item_code = r.item_code and r.urule_code = 'LCSPAINTSZ'
# python 3.6 way is shorter
from pathlib import Path
# do you know the basename of the file you just read?
filename_xls = 'file123'
# is cell read as string?
cell_content_str = """select ip.item_code , ip.gla_code_ar ,r.ruleit_text LCSPAINTSZ
from item i , itemplant ip , ruleit r where i.item_code = ip.item_code
and ip.plant_code = '14' and i.item_active = 1 --and i.item_code = '457332'
and i.item_code = r.item_code and r.urule_code = 'LCSPAINTSZ'"""
def make_sql_filename(basename: str):
return f'{basename}.sql'
def write_to_sql_file(content: str, basename: str):
Path(make_sql_filename(basename)).write_text(content)
if __name__ == '__main__':
write_to_sql_file(cell_content_str, filename_xls)
Generally it looks too simple to be an answer, perhaps I'm missing somehting from the question. 通常,它看起来太简单而不能作为答案,也许我在这个问题上漏了一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.