读取 Excel 单元格并将内容复制到 txt 文件 [英] Read Excel Cells and Copy content to txt file
问题描述
我目前正在使用 RapidMiner,并试图将我的 RapidMiner 结果复制到 xlsx 文件中,以便使用 python 进行进一步处理.我在 A 列 (A1-A1500) 中有纯文本,在 C 列 (C1-C1500) 中有相应的文件名.现在我的问题:是否有可能(我正在考虑 xlrd 模块)读取 A 列中每个单元格的内容并将其打印到新创建的 txt 文件中,文件名在相应的 C 列中给出?
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?
因为我从来没有使用过 xlrd 模块,所以我现在有点迷茫......
As I have never worked with the xlrd module before I am a bit lost at the moment...
推荐答案
我可以推荐 openpyxl 用于与 .xlsx 处理相关的每个任务.
I can recommend openpyxl for every tasks concerning .xlsx handling.
对于您的要求:
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)
这篇关于读取 Excel 单元格并将内容复制到 txt 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!