Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write Excel data of Python Automation Office

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces Python Office Automation Excel data how to write related knowledge, detailed and easy to understand, simple and fast operation, has a certain reference value, I believe everyone read this Python Office Automation Excel data how to write articles will have some gains, let's take a look at it.

Excel write- xlsxwriterxlsxwriter installation

Installation method:

pip install xlsxwriter

If the installation fails or the installation speed is too slow, you can change the source address of the domestic image: pip install -i https://pypi.tuna.tsinghua.edu.cn/simple xlsxwriter

Import:

import xlsxwriterxlsxwriter common function introduction initialization excel object book = xlsxwriter.Workbook() #Create excel object sheet = book.add.sheet(workbook name) #Generate workbook name of excel object Get workbook function name Description parameter xlsxwriter.Workbook() Generate excel object excel file name add_worksheet() Add workbook name sheet.write() Write content Row index, column index, content book.close() Close excel object None

The code example is as follows:

# coding:utf-8import xlsxwriter # pip install xlsxwriterexcel = xlsxwriter.Workbook('write.xlsx') #initialize excel object book = excel.add_worksheet ('study ') #Add workbook title ="'Name ', ' Gender','Age',' Grade','Grade'"] #define the first line of write.xlsx for index, data in enumerate(title): # for loop Use enumeration function to write the contents of title to the first line of "write.xlsx" book.write(0, index, data)excel.close()

The results are as follows:

Small combat

Writes the contents of the study.xlsx file in the project to write.xlsx

The code example is as follows:

# coding:utf-8import xlsxwriter # pip install xlsxwriterimport xlrd# excel = xlsxwriter.Workbook('write.xlsx') #initialize excel object # book = excel.add_worksheet ('study ') #Add Workbook ## title ="'Name ', ' Gender','Age',' Grade','Grade'"] #define the first line of write.xlsx ## for index, data in enumerate(title): # for loop Use enumeration function to write the contents of title to the first line of "write.xlsx"# book.write(0, index, data)# excel.close()def read(): #Define a read function to read "study.xlsx" file result = [] excel = xlrd.open_workbook('study.xlsx') book = excel.sheet_by_name ('student book') for i in book.get_rows(): content = [] for j in i: content.append(j.value) result.append(content) return resultdef write(content): #Define a write function to write the contents read to "study.xlsx" to the file "write.xlsx" excel = xlsxwriter.Workbook('write.xlsx') book = excel.add_worksheet('study') for index, data in enumerate(content): print(data) #debug print every line written for sub_index, sub_data in enumerate(data): # print(sub_index, sub_data) book.write(index, sub_index, sub_data) excel.close()if __name__ == '__main__': result = read() write(result)

The results are as follows:

About "Python Office Automation Excel data how to write" The content of this article is introduced here, thank you for reading! I believe everyone has a certain understanding of the knowledge of "how to write Excel data in Python automation office." If you still want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report