简体   繁体   English

通过 Python 将 Excel 工作簿的行拆分为多个 Excel 文件

[英]Splitting an Excel workbook's rows into multiple excel files through Python

I'm doing a lot of Excel stuff as part of my internship, and one of the things I have to do was separate a master excel file I made into multiple ones作为实习的一部分,我正在做很多 Excel 工作,而我要做的一件事就是将我制作的主 Excel 文件分成多个文件

It has multiple columns, for name, phone number, etc. The idea is to separate the master file into several, one file per name in the name column, while still keeping the first row (the one that identifies the columns).它有多个列,用于名称、电话号码等。想法是将主文件分成几个,名称列中每个名称一个文件,同时仍保留第一行(标识列的行)。 What is really confusing me is that, if a name repeats in the name column, the created file needs to have all the rows with that name.真正让我困惑的是,如果名称列中重复的名称,则创建的文件需要具有该名称的所有行。

I used this code from here Python splitting an Excel workbook .我使用这里的代码 Python 拆分 Excel 工作簿 I altered it so it saved one new file every row, the problem is that it also separates the row that identifies the columns, and the rows with the same name.我对其进行了更改,因此它每行保存一个新文件,问题是它还将标识列的行和具有相同名称的行分开。

This is my first time asking here and I've hit a dead end, so I would appreciate some help.这是我第一次在这里问,我遇到了死胡同,所以我会很感激一些帮助。

You can aproach it with pandas.你可以用熊猫来接近它。 But I would not encourage to make severeal one line files, you would be better corvered with json files.但我不鼓励制作严格的单行文件,最好使用 json 文件。

You can try something like this:你可以尝试这样的事情:

import pandas as pd
df = pd.read_excell('your_file.xlsx')
for index, row in df.itterrows():
  pd.DataFrame([row.to_dict()]).to_excel('output_file.xlsx',index=False)

You still need to rename dinamicaly the file name, but it works您仍然需要重新命名文件名,但它可以工作

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

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