Get the App
SLTechnology News&Howtos  ›  Development  › 

How to write Excel data of Python Automation Office

Shulou Source: shulou.com Published: 2022-05-31 16:42:43 09月14日 Update

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.

Tags: Content objects work functions data office automation files names knowledge generation code name age gender grades examples grades articles results Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Docker NVidia Shulou Information MariaDB