我需要一個簡單的方法來周圍設置多個單元格邊框,就像這樣: 蟒蛇XlsxWriter設置邊框多個單元
我所發現的是1個單元格的邊框,合併單元格,這不是我所需要的。
我期待這樣的事情:
worksheet.range_border(first_row, first_col, last_row, last_col)
有沒有一種方式,可以做到這一點(即不涉及設置top_border,bottom_border, left_border,right_border每個單元獨立)?
我需要一個簡單的方法來周圍設置多個單元格邊框,就像這樣: 蟒蛇XlsxWriter設置邊框多個單元
我所發現的是1個單元格的邊框,合併單元格,這不是我所需要的。
我期待這樣的事情:
worksheet.range_border(first_row, first_col, last_row, last_col)
有沒有一種方式,可以做到這一點(即不涉及設置top_border,bottom_border, left_border,right_border每個單元獨立)?
XlsxWriter是由我過去的工作更容易1000倍(感謝約翰!),但格式與它的細胞可以耗時一個真棒模塊。我有一些輔助函數可以用來做這樣的事情。
首先,你需要能夠通過添加屬性到現有的格式創建一個新的格式:
def add_to_format(existing_format, dict_of_properties, workbook):
"""Give a format you want to extend and a dict of the properties you want to
extend it with, and you get them returned in a single format"""
new_dict={}
for key, value in existing_format.__dict__.iteritems():
if (value != 0) and (value != {}) and (value != None):
new_dict[key]=value
del new_dict['escapes']
return(workbook.add_format(dict(new_dict.items() + dict_of_properties.items())))
現在建立了與該函數的:
def box(workbook, sheet_name, row_start, col_start, row_stop, col_stop):
"""Makes an RxC box. Use integers, not the 'A1' format"""
rows = row_stop - row_start + 1
cols = col_stop - col_start + 1
for x in xrange((rows) * (cols)): # Total number of cells in the rectangle
box_form = workbook.add_format() # The format resets each loop
row = row_start + (x // cols)
column = col_start + (x % cols)
if x < (cols): # If it's on the top row
box_form = add_to_format(box_form, {'top':1}, workbook)
if x >= ((rows * cols) - cols): # If it's on the bottom row
box_form = add_to_format(box_form, {'bottom':1}, workbook)
if x % cols == 0: # If it's on the left column
box_form = add_to_format(box_form, {'left':1}, workbook)
if x % cols == (cols - 1): # If it's on the right column
box_form = add_to_format(box_form, {'right':1}, workbook)
sheet_name.write(row, column, "", box_form)
這對我很有幫助,我基本上覆制了你的代碼以在一盒單元格周圍添加邊框和其他一些樣式。一個附錄是您當前的示例使用這些格式,但一旦box(..)函數運行,您將失去對Format對象的引用。 我不得不創建一個映射,並從盒子的函數返回這麼寫文本的特定數據的單元格時,盒子呼叫這樣,他們不會失去其格式之內時,我寫了片+ WB我以後可以重複使用的格式對象。 – Matteius
Matteius是對的。 xlsxwriter的這一方面很煩人。我已經更新了我如何處理這個問題。看看這個問題,爲了更好的說明:http://stackoverflow.com/questions/37907406/rewriting-some-functions-for-xlsxwriter-box-borders-from-python-2-to-python-3/39608582#39608582 – aubaub
請看看這個答案http://stackoverflow.com/a/32964050/1731460 – pymen