问题
I'm trying to create a series of spreadsheets for my office based on criteria from a larger spreadsheet we have. This could very easily be done by hand, but since I am new to programming, I thought this could be a fun exercise to see if I could do it with python using the Openpyxl module.
Basic problem: I have a list of countries people are traveling to, and I have rows with people's names and other data, including the name of the country each person is traveling to. The country each person is going to is in the first column. For each country, I want to make a separate workbook that grabs the row of data for each person going to that country. For other purposes in my office, each list should be saved as a separate .xlsx file, not simply a new sheet all in one workbook.
I am very new to programming, but here is how I'm trying to do it so far:
import openpyxl
from openpyxl import load_workbook
from openpyxl import Workbook
def create_roster(Country_name):
roster_book = Workbook()
ws2 = roster_book.active
for row in cell_rows:
if row[0] == Country_name:
ws2.append(row)
roster_book.save('C:\\....\\{}.xlsx'.format(Country_name))
countries = ('Au','Brazil','Costa R','Cuba','France','Germ','Ghana','Greece','Hungary','Italy','Japan','Korea','Peru','Romania','S.A.','Switz','Thai')
wb = load_workbook('C:\\....\\Big_list.xlsx', data_only=True)
ws = wb.active
cell_rows = []
for row in ws.values:
cell_rows.append(tuple(row))
for country in countries:
create_roster(country)
When I run this code, an error message I have never got before appears and says, "Exception Unhandled: got invalid input value of type < class 'xml.etree.ElementTree.Element'>, expected string or Element."
A search for this error message has so far not been fruitful for me.
What is more peculiar is that I am not being shown the line number the error is stemming from as usual (I am using Visual Studio 2019), and instead the IDE module displays the text "Frame not in module. The current stack frame was not found in a loaded module. Source cannot be shown for this location."
Another bit of relevant info on this error is that in the folder where I am attempting to create all these .xlsx files, the first one in my list, Au.xlsx, appears in the folder I am trying to save them all to. But when I try to open this file using Excel, it is unreadable.
I cannot tell what is wrong with the way I am trying to do this. Is this the wrong way to go about trying to create multiple custom .xlsx files? Any insight or advice here would be much appreciated.
回答1:
df = pd.read_excel("Filename.xlsx")
#make a list of all unique country names
# or you can just read that specific cloumn and remove duplicate and pass it into a list
list = [All unique country names]
for i in list:
# apply str func to I/P for String Comparsion
i = str(i)
leng = len(i)
try:
#make slice of dataframe containing only country = i
df1= df[df.iloc[:, column_number_of_countries_column].str[:leng]== i]
except:
# pass if country does not exists
continue
#save df slice to excel sheet with name as Country
df1.to_excel(i+".xlsx", index= False)
print(" Sheet {} saved successfully!".format(i))
print("File Saved!")
来源:https://stackoverflow.com/questions/60497136/using-openpyxl-to-create-multiple-custom-spreadsheets