Создание новых файлов Excel из данных каждой электронной таблицы - PullRequest
0 голосов
/ 24 апреля 2019

Мне просто нужен совет о том, как подойти к проблеме.Мне нужно создать новый файл для каждой электронной таблицы в файле Excel (он имеет около 80 листов), который содержит соответствующие данные электронных таблиц.

Можно ли использовать библиотеку xlrd для чего-то подобного?

Ответы [ 2 ]

0 голосов
/ 24 апреля 2019

Вы можете решить свою проблему с помощью библиотеки Pandas:

#Import pandas module (if you don't have it open the command line and run: pip install pandas)
import pandas as pd

#Read the Excel file to a Pandas DataFrame
#Note: change "file_name" to the name of your Excel file
#This will give you a dictionary containing one DataFrame per each sheet on the Excel file 
data = pd.read_excel("file_name.xlsx", sheet_name=None)

#Iterate through the dictionary
count = 0
for d in data:
    #Give different names according to the sheet where data came from
    count = count + 1
    name = "sheet_" + str(count)
    #Save to an Excel file
    d.to_excel(name + ".xlsx")
0 голосов
/ 24 апреля 2019

Хорошо, вот одно решение

import pandas as pd

df1 = pd.read_excel('C:\\Users\\xy\\Desktop\\Report.xls', 'Sheet 1',decimal=',')

df2 = pd.read_excel('C:\\Users\\xy\\Desktop\\Report.xls', 'Sheet 2',decimal=',')

df3 = pd.read_excel('C:\\Users\\xy\\Desktop\\Report.xls', 'Sheet 3',decimal=',')

df4 = pd.read_excel('C:\\Users\\xy\\Desktop\\Report.xls', 'Sheet 4',decimal=',')

df5 = pd.merge(df1, df2, df3, df4)
...