Read Excel Cells and Copy content to txt file

安稳与你 提交于 2020-01-30 03:15:48

问题


I am working with RapidMiner at the moment and am trying to copy my RapidMiner results which are in xlsx files to txt files in order to do some further processing with python. I do have plain text in column A (A1-A1500) as well as the according filename in column C (C1-C1500). Now my question: Is there any possibility (I am thinking of the xlrd module) to read the content of every cell in column A and print this to a new created txt file with the filename being given in corresponding column C?

As I have never worked with the xlrd module before I am a bit lost at the moment...


回答1:


I can recommend openpyxl for every tasks concerning .xlsx handling.

For your requirements:

from openpyxl import *
import os    

p = 'path/to/the/folder/with/your/.xlsx'
files = [_ for _ in os.listdir(p) if _.endswith('.xlsx')]

for f in files:

     wb = load_workbook(os.path.join(p, f))
     ws = wb['name_of_sheet']
     for row in ws.rows:
         with open(row[2].value+'.txt', 'w') as outfile:
              outfile.write(row[0].value)



回答2:


Good day! So, I'm not sure I understand your question correctly, but have you tried a combination of Read Excel operator with the Loop Examples operator? Your loop subprocess could then use Write CSV operator or similar.




回答3:


Thanks to @corinna the final code is:

from openpyxl import *
import os


p = r'F:\Results'
files = [_ for _ in os.listdir(p) if _ .endswith('.xlsx')]
os.chdir(r"F:\Results")
for f in files:

    file_location = load_workbook(os.path.join(p, f))
    sheet = file_location['Normal']
    for row in sheet.rows:
        with open(row[2].value + '.txt', "w") as outfile:
            outfile.write(row[0].value)


来源:https://stackoverflow.com/questions/39512166/read-excel-cells-and-copy-content-to-txt-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!