简体   繁体   中英

how to modify the each cell in the excel using openpyxl

在此处输入图像描述

I want to read data from input excel sheet and write it in a outut excel sheet in the format shown below. 在此处输入图像描述

try this: (with the below code you can read Book1.xlsx, convert the excel file to list of lists , and write list of lists in excel file(Book2.xlsx))

EDIT : I add start_cloumn and start_row as your request for set where start row and column for adding 0x .

import pandas as pd
import xlsxwriter

df = pd.read_excel(r'Book1.xlsx')

lst = [df.columns.values.tolist()] + df.values.tolist()
 
start_cloumn = 4 # added this line on EDIT
start_row = 2 # added this line on EDIT

with xlsxwriter.Workbook('Book2.xlsx') as workbook:
    worksheet = workbook.add_worksheet()

    for row_num, data in enumerate(lst):
        if row_num >= start_row:
            data = ["0x" + str(d) if column_num >= (start_cloumn-1) else str(d) for column_num, d in enumerate(data)]
        worksheet.write_row(row_num, 0, data)

output:

在此处输入图像描述


在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM