簡體   English   中英

使用openpyxl(python)在列中進行HEX-BIN轉換的多行寫入

[英]Multi-row writing of HEX-BIN conversion in a column with openpyxl (python)

我的目標是將單元格的值(對於 E 列中某個范圍內的每一行)從 HEX 轉換為 BIN 並將 BIN 寫入每行的 N 列中。 轉換本身以及將 BIN 值寫入某個單元格都沒有問題。 但是我在通過行進行迭代時遇到問題。 澄清一下:我希望 E1 中的 HEX 代碼以 N1 中的 BIN 代碼打印,E2 中的值以 N2 打印,依此類推

A  B  C D  E  F  G  H  I  J  K  L  M  N
           90                         ‭10010000‬
           8A                         ‭10001010‬
           ..                         ....

這是我的代碼:

theFile = openpyxl.load_workbook('T013.xlsx')
allSheetNames = theFile.sheetnames 
print("All sheet names {} " .format(theFile.sheetnames)) 
sheet = theFile.active

for row in sheet.iter_rows(min_row=1, max_row=1210,values_only = True): 
    for cell in sheet["E"]: 
        if cell.value is None:
            print("Blank")
        else: 
            inputHEX = str(cell.value) 
            res = "{0:08b}".format(int(inputHEX,16))
            print(res)
            for x in sheet["N1:N1210"]:
                sheet.cell(row=x, column=1).value = str(res) #same error as with res without str 
theFile.save("C:\\Users\\...\\Adapted_T013.xlsx") 

我得到的錯誤是:

C:\Users\...\Desktop\Practical Part\CAN Python>python ExcelDecode.py
All sheet names ['T013']
Blank
11100001
Traceback (most recent call last):
  File "ExcelDecode.py", line 42, in <module>
    sheet.cell(row=x, column=1).value = res
  File "C:\Users\...\AppData\Local\Programs\Python\Python38-32\lib\site-packages\openpyxl-3.0.3-py3.8.egg\openpyxl\worksheet\worksheet.py", line 235, in cell
TypeError: '<' not supported between instances of 'tuple' and 'int'`

我嘗試使用元組轉換函數,但這並沒有改變結果。

如果你能告訴我克服這個錯誤的方法,那就太好了。 謝謝!

正確實施:

theFile = openpyxl.load_workbook('T013.xlsx')
allSheetNames = theFile.sheetnames 
print("All sheet names {} " .format(theFile.sheetnames)) 
sheet = theFile.active

for row in sheet.iter_rows(min_row=1, max_row=1210,values_only = True): 
    for cell in sheet["E"]:
        if cell.value is not None:
            inputHEX = str(cell.value)
            res = "{0:08b}".format(int(inputHEX,16))
            sheet.cell(row=cell.row, column=14).value = res
    break
theFile.save("C:\\Users\\..\\Adapted_T013.xlsx") 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM