简体   繁体   English

Python程序读取Excel文件并将其sql复制到新的文本文件中

[英]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.

相关问题 Python程序读取文本文件并在Excel中制表 - Python program to read text files and tabulate in excel Python程序不会读取文本文件中的新行 - Python program won't read new line in text file Python:将具有行标题的文本文件读取到新的CSV / Excel中 - Python: Read text file with column headers in rows into new CSV/Excel 将文本文件转换为 Excel 文件的 Python 程序 - Python program to convert a text file into an Excel file 使用 Python 3.x 根据存储在文本文件中的文件名将文件复制到新目录 - Copy files to a new directory according to file names stored in a text file with Python 3.x 如何逐行读取多个文本文件并在每个文件后发送到excel移动到新列? - How to read multiple text files line by line and send to excel moving to a new column after every file? Python - 在文件打开时读取 Excel 文件 - Python - Read Excel files while file is open 如何让程序“读取”并复制文本文件的内容? - How do I let the program "read" and copy the content of a text file? Python 3 文本文件 - 我想将用户输入添加到文件 1 中每行文本的末尾,然后将新行复制到文件 2 中 - Python 3 text files - I want to add add user input to the end of each line of text in file 1 then copy the new lines into file 2 将特定行从多个文本文件复制到Excel文件 - Copy specific lines from multiple text files to an excel file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM