繁体   English   中英

从本地存储读取和操作 xlsx 文件,并将单元格中的一些值提取到 .txt 和 .csv 文件中

[英]Read and manipulate xlsx file from local storage and extract some of the values from cells into .txt and .csv files

读取 excel 文件并使用 openpyxl 将详细信息提取到 new.txt 文件中

我是openpyxl的新手,昨天才开始。 我需要从 excel 文件有 500+ 行和 50+ 列的本地存储中提取 excel 文件中的数据。 我想将一些特定的单元格或列或行提取到 .txt 和 .csv 文件中。

我找不到在此代码中添加的错误在哪里

from openpyxl import *
import os    

path = 'F:\\mis'
files = [i for i in os.listdir(path) if i.endswith('.xlsx')]

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

所以我有这个带有一些学生姓名的示例 xlsx 文件:

在此处输入图像描述

我使用此代码根据学号提取数据:

import openpyxl

roster = openpyxl.load_workbook('roster.xlsx')
sheet = roster.active

st_id = int(input('Enter student ID: '))
for row in sheet.rows:
    if row[0].value == st_id:
        print("First Name: {}\nLast Name: {}".format(row[1].value,row[2].value))
import pandas as pd
df=pd.read_excel("F:\\mis\\CASHE.xlsx")
cus_id=input("Please enter your customer id:")
cashe=['customer_id', 'customer_name', 'login_email_id', 'login_source', 'New_date', 'customer_status_name']

if cus_id == '':
    df1=df[cashe]
else:
    df1=df[cashe].loc[df['customer_id']==int(cus_id)]

df1.to_csv('Cashe1.csv')
print("csv printed with values")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM