python openpyxl篩選某些列的操作

2022-09-26 00:03:10 字數 1348 閱讀 1004

由於要複製excel 的某些單元格格式,需要對合併的單元格選出符合條件的

然後在新表單中

wbsheet_new.merge_cells(cell2)

wbsheet_new為新表單,cell2為篩選後保留的單元格,表達為i24:j24,k24:l24這樣的格式

先正則篩選,篩選的結果為[(『ao', 『ap')]這種list包含元組的表達方式,再用result[0][0]提取出第乙個元素,

column_index_from_string(result[0][0])>=15

from openpyxl.utils import get_column_letter, column_index_from_string

import re

s=['ak23:al23',

'am23:an23',

'ao23:ap23',

'aq23:ar23'

'b24:c24',

'd24:f24',

'g24:h24',

'i24:j24',

'k24:l24',

'm24:n24',

'q24:r24',

's24:u24',

'v24:w24',

'x24:y24',

'z24:aa24',

'ab24:ac24',

'af24:ag24',

'ah24:aj24',

'ak24:al24',

'am24:an24',

'ao24:ap24',]

for si in s:程式設計客棧

result=re._compile(r'(\w+程式設計客棧63;)\d+:(\w+?)\d+',flags=0).findall(si)

print (result)

if co程式設計客棧ndex_from_string(result[0][0])>=15:

print('去掉%s'%result[0][0])

補充:python openpyxl 獲取合併的單元格列表,篩選解除單元格合併

#獲取該錶所有合併單元格資訊並遍歷

for i in ws.merged_cell_ranges:

#獲取合併單元格的開始行、列,結束行、列

r1, r2, c1, c2 = i.min_row, i.max_row, i.min_col, i.max_col

#解除合併

ws2.unmerge_cells(start_row = r1, end_row = r2, start_column = c1, end_column = c2)

本文標題: python openpyxl篩選某些列的操作

本文位址:

python openpyxl設定excel格式

from openpyxl import workbook 新建時匯入這個 from openpyxl import load workbook 讀取時匯入這個 from openpyxl.styles import font,alignment 設定單元格格式 from openpyxl.util...

素數篩 埃氏篩 線性篩 區間篩)

顧名思義,素數篩就是用來篩素數的。1.埃氏篩 o nloglogn 對於一般 不毒瘤 的素數題,埃氏篩就夠了 原理 任何合數都有小於自身的質因數 內容 對於每乙個素數將它的 2 i x i i全部標記為1,使得所有的合數全被標記 不足 合數會被標記素因數次,不夠高效 void prime int x...

素數篩法(素數篩 線性篩)

求素數的方法在現階段可以總結為三種 這種方法最為簡單但效率太低,經過優化時間複雜度最低是o n sqrt n 輸入乙個n,輸出n以內所有素數 include intprime int n if flag 0 優化 printf d i intmain 素數篩法原理 2是素數,那麼2的所有倍數都是合數...