Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
219 views
in Technique[技术] by (71.8m points)

python 3.x - Removing Blank Columns from an excel file

I have written a program to download data from an API, and parse the json response into an excel (xlsx) sheet in multiple worksheets.

However, depending on user's choice on what attributes to be saved, there can be empty columns in the excel. Is there any way to delete empty columns from all worksheets in the excel file?

I can do this through pandas, however, that is not possible in this case, as pandas and openpyxl library breaks the GUI based on the python script?

Is there any other way to implement this?

Edit:

Current Output:

+----------+----------+--+----------+----------+
| Column A | Column B |  | Column D | Column E |
+----------+----------+--+----------+----------+
| A1       | B1       |  | D1       |          |
| A2       |          |  | D2       |          |
| A3       | B3       |  |          | E3       |
+----------+----------+--+----------+----------+

Desired Output:

+----------+----------+----------+----------+
| Column A | Column B | Column D | Column E |
+----------+----------+----------+----------+
| A1       | B1       | D1       |          |
| A2       |          | D2       |          |
| A3       | B3       |          | E3       |
+----------+----------+----------+----------+

Current Code for removing blank columns:

excel_file = pd.ExcelFile(path, engine='openpyxl')
df = pd.read_excel(excel_file,header=None, sheet_name=None)
writer = pd.ExcelWriter(finalpath,engine='openpyxl') 
for key in df:
    sheet= df[key].dropna(how="all").dropna(1,how="all")
    sheet.to_excel(writer, key,index=False, header=False )
writer.save()

Need to replace this code with something which don't references pandas library.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...