當前位置:首頁 » 編程語言 » pythonstyle

pythonstyle

發布時間: 2023-05-17 20:28:21

『壹』 python xlwt怎麼給單元格中部分字設置顏色

#coding=utf-8

import xlwt

#file = xlwt.Workbook(encoding = 'utf-8')

wbk = xlwt.Workbook(encoding = 'utf-8')

sheet = wbk.add_sheet('sheet 1', cell_overwrite_ok=True)

#這樣表單就被創建了,寫入數據也很簡單:

# indexing is zero based, row then column

#sheet2 = wbk.add_sheet('sheet 2', cell_overwrite_ok=True)

style = xlwt.XFStyle()

font = xlwt.Font()

font.name = 'SimSun' # 指定「宋體」

style.font = font

sheet.write(0,1,'test text')

#sheet.write(1,2,'excel讀寫測試',style)

sheet.write(1,2,'excel讀寫測試',xlwt.easyxf('font: height 240, name Arial, colour_index black, bold off, italic on; align: wrap on, vert centre, horiz left;')

)

#之後,就可以保存文件(這里不需要想打開文件一樣需要close文件):

wbk.save('test4.xls')
請看代碼示例二:
## Startup Code Begin
import xlwt
from xlwt import *
import string
import os
from datetime import datetime
import time
from time import gmtime, strftime
current_date = strftime("%b %d %Y (%H:%M)")
## Startup Code End

## Moles Begin
def _size_col(sheet, col):
return sheet.col_width(col)

def _size_row(sheet, row):
return sheet.row_height(row)
## Moles End

## Style variable Begin
tittle_style = xlwt.easyxf('font: height 400, name Arial Black, colour_index blue, bold on; align: wrap on, vert centre, horiz center;' "borders: top double, bottom double, left double, right double;")
subtittle_left_style = xlwt.easyxf('font: height 240, name Arial, colour_index brown, bold on, italic on; align: wrap on, vert centre, horiz left;' "borders: top double, bottom double, left double;")
subtittle_right_style = xlwt.easyxf('font: height 240, name Arial, colour_index brown, bold on, italic on; align: wrap on, vert centre, horiz left;' "borders: top double, bottom double, right double;")
subtittle_top_and_bottom_style = xlwt.easyxf('font: height 240, name Arial, colour_index black, bold off, italic on; align: wrap on, vert centre, horiz left;' "borders: top double, bottom double;")
blank_style = xlwt.easyxf('font: height 650, name Arial, colour_index brown, bold off; align: wrap on, vert centre, horiz left;' "borders: top double, bottom double, left double, right double;")
normal_style = xlwt.easyxf('font: height 240, name Arial, colour_index black, bold off; align: wrap on, vert centre, horiz left;' "borders: top double, bottom double, left double, right double;")
## Style variable End

## Mole Add Begin
def print_blank_line(A, B, C, D):
ws.write_merge(A, B, C, D, "", xlwt.easyxf('font: height 240, name Arial, colour_index black, bold off, italic on; align: wrap on, vert centre, horiz left;'))
## Mole Add End

## Variable Begin
Excel = xlwt.Workbook()
## Variable End

## Sheet Name Begin
ws = Excel.add_sheet('Test Sheet')
## Sheet Name End

## Column Width Determine Begin
for A in range(100):
ws.col(A).width = 600
## Column Width Determine Begin

## Tittle Picture Begin
ws.write_merge(0, 0, 0, 42, "", xlwt.easyxf('font: height 700, name Arial, colour_index brown, bold off; align: wrap on, vert centre, horiz left;'))
##ws.insert_bitmap('test.bmp', 0, 0, 60, 5, 1.75, 1)
## Tittle Picture End

## Subtittle Write Begin
# Variable Begin
job_name = "Nicolas_Parametric"
fab_name = "Nicolas_Parametric"
printed_by = "Nic"
printed_date = current_date
# Variable End

border_type = ["dashed", "double", ""]

ws.write_merge(1, 1, 0, 6, "Job Name:", xlwt.easyxf('font: height 240, name Arial, colour_index black, bold on, italic on; align: wrap on, vert centre, horiz left;' "borders: top double, bottom double, left double;"))
ws.write_merge(1, 1, 7, 21, job_name, xlwt.easyxf('font: height 240, name Arial, colour_index black, bold off, italic on; align: wrap on, vert centre, horiz left;' "borders: top double, bottom double;"))
ws.write_merge(2, 2, 0, 6, "Fab Name:", xlwt.easyxf('font: height 240, name Arial, colour_index black, bold on, italic on; align: wrap on, vert centre, horiz left;' "borders: top double, bottom double, left double;"))
ws.write_merge(2, 2, 7, 21, fab_name, xlwt.easyxf('font: height 240, name Arial, colour_index black, bold off, italic on; align: wrap on, vert centre, horiz left;' "borders: top double, bottom double;"))

ws.write_merge(1, 1, 22, 27, "Printed By:", xlwt.easyxf('font: height 240, name Arial, colour_index black, bold on, italic on; align: wrap on, vert centre, horiz left;' "borders: top double, bottom double;"))
ws.write_merge(1, 1, 28, 42, printed_by, xlwt.easyxf('font: height 240, name Arial, colour_index black, bold off, italic on; align: wrap on, vert centre, horiz left;' "borders: top double, bottom double, right double;"))
ws.write_merge(2, 2, 22, 27, "Printed Date:", xlwt.easyxf('font: height 240, name Arial, colour_index black, bold on, italic on; align: wrap on, vert centre, horiz left;' "borders: top double, bottom double;"))
ws.write_merge(2, 2, 28, 42, printed_date, xlwt.easyxf('font: height 240, name Arial, colour_index black, bold off, italic on; align: wrap on, vert centre, horiz left;' "borders: top double, bottom double, right double;"))

print_blank_line(3, 3, 0, 42)
ws.write_merge(4, 4, 0, 42, "Report Name", xlwt.easyxf('font: height 400, name Arial, colour_index red, bold on, italic on, underline on; align: wrap on, vert centre, horiz center;'))
print_blank_line(5, 5, 0, 42)

ws.write_merge(6, 6, 0, 1, "#", xlwt.easyxf('font: height 240, name Arial, colour_index black, bold on, italic on; align: wrap on, vert centre, horiz left;' "borders: top double, bottom double, left double, right double;"))
ws.write_merge(6, 6, 2, 7, "Piecemark", xlwt.easyxf('font: height 240, name Arial, colour_index black, bold on, italic on; align: wrap on, vert centre, horiz left;' "borders: top double, bottom double, left dashed, right double;"))
ws.write_merge(6, 6, 8, 10, "Qty", xlwt.easyxf('font: height 240, name Arial, colour_index black, bold on, italic on; align: wrap on, vert centre, horiz left;' "borders: top double, bottom double, left dashed, right double;"))
ws.write_merge(6, 6, 11, 20, "Size", xlwt.easyxf('font: height 240, name Arial, colour_index black, bold on, italic on; align: wrap on, vert centre, horiz left;' "borders: top double, bottom double, left dashed, right double;"))
ws.write_merge(6, 6, 21, 34, "Status", xlwt.easyxf('font: height 240, name Arial, colour_index black, bold on, italic on; align: wrap on, vert centre, horiz left;' "borders: top double, bottom double, left dashed, right double;"))
ws.write_merge(6, 6, 35, 42, "Date", xlwt.easyxf('font: height 240, name Arial, colour_index black, bold on, italic on; align: wrap on, vert centre, horiz left;' "borders: top double, bottom double, left dashed, right double;"))
## Subtittle Write End

normal_font = xlwt.easyxf('font: height 240, name Arial, colour_index black; align: wrap on, vert centre, horiz left;' "borders: top dashed, bottom dashed, left double, right double;")

for A in range(98):
ws.write_merge(7 + A, 7 + A, 0, 1, "", normal_font)
ws.write_merge(7 + A, 7 + A, 2, 7, "", normal_font)
ws.write_merge(7 + A, 7 + A, 8, 10, "", normal_font)
ws.write_merge(7 + A, 7 + A, 11, 20, "", normal_font)
ws.write_merge(7 + A, 7 + A, 21, 34, "", normal_font)
ws.write_merge(7 + A, 7 + A, 35, 42, "", normal_font)

## File Save As Begin
Excel.save('Test.xls')
## File Save As End

#### Start File Begin
##os.startfile("C:/Documents and Settings/dev01/Desktop/Test.xls")
#### Start File End

『貳』 python-docx怎麼給表格自定義樣式

1、word表格樣式的設置
from docx import *
document = Document()
table = document.add_table(3, 3, style="Medium Grid 1 Accent 1")
heading_cells = table.rows[0].cells
heading_cells[0].text = '第一列內容'
heading_cells[1].text = '第二列內容'
heading_cells[2].text = '第三列內容'
document.save(r"d:\demo.docx")

2、獲取所有word表格樣敬拿陸式
from docx.enum.style import WD_STYLE_TYPE
from docx import *
document = Document()
styles = document.styles

#生成亮頃所有表樣式
for s in styles:
if s.type == WD_STYLE_TYPE.TABLE:
document.add_paragraph("表格樣式 : "+ s.name)
table = document.add_table(3,3, style = s)
heading_cells = table.rows[0].cells
heading_cells[0].text = '第一列內容'
heading_cells[1].text = '第二列內容'
heading_cells[2].text = '第三列內容'敏薯
document.add_paragraph("\n")

『叄』 selenium3+python可以更改元素的style嗎

要點擊一個 Menu 時需要對 SPAN 元素進行操作,後來喊啟侍在 Selenium 的論壇中找鄭吵到了答案。示例代碼如下:
1

2 click
3 //span[contains(text(),'String')]
4
5
在實際使用時,把 String 部分替換為實際的 text 就可以了。
另外一旁汪種做法是不行的:

1
2 click
3 //span[@text='String']
4
5

『肆』 Python 插曲:編碼風格是如何使用的呢

插曲:編碼風格
此時你已經可以寫一些更長更復雜的 Python 程序,是時候討論一下 編碼風格 了。大多數語言可以寫(或者更明白的說, 格式化 )作幾種不同的風格。有些比其它的更好讀。讓你的代碼對別人更易讀是個好想法,養成良好的編碼風格對此很有幫助。
對於 Python,PEP 8 引入了大多數項目遵循的風格指導。它給出了一個高度可讀,視覺友好的編碼風格。每個 Python 開發者都應該讀一下,大多數要點都會對你有幫助:
使用 4 空格縮進,而非 TAB
在小縮進(可以嵌套更深)和大縮進(更易讀)之間,4空格是一個很好的折中。TAB 引發了一些混亂,最好棄用
折行以確保其不會超過 79 個字元
這有助於小顯示器用戶閱讀,也可以讓大顯示器能並排顯示幾個代碼文件
使用空行分隔函數和類,以及函數中的大塊代碼
可能的話,注釋獨佔一行
使用文檔字元串
把空格放到操作符兩邊,以及逗號後面,但是括弧里側不加空格:a = f(1, 2) + g(3, 4)
統一函數和類命名
推薦類名用 駝峰命名, 函數和方法名用 小寫_和_下劃搭清鍵線。總是用 self 作為方法的第一個參數(關於類和方法的知識詳見 初識類 )
不要使用花哨的編碼,如果你的代碼的目的是要在國際化環境。Python 的默認情況下,UTF-8,甚至普通的 ASCII 總是知巧工作的最好
同樣,也不正則要使用非 ASCII 字元的標識符,除非是不同語種的會閱讀或者維護代碼。

『伍』 Python命名規則與注釋風格

名稱不要縮寫!!!

名稱不要縮寫!!!

名稱不要縮寫!!!

包命名:小寫,下劃線_連接單詞

模塊命名:小寫,下劃線_連接單詞

類命名:雙駝峰,MyClass

函數命名:小寫,下劃線_連接單詞

全局變數:每個字母大寫

注釋很重要,要註明函數或類的功能,參數的數據類型、含義等。

注釋模板:

注釋的作用:

Python有句話是do not repeat yourself,即不要做重復的事,因此出現了類或函數的封裝和繼承,基於此可提高代碼的重復利用性。

『陸』 python語言屬於什麼語言

Python是一種跨平台的計算機程序設計語言。是一種面向對象的動態類型語言,最初被設計用於編寫自動化腳本(shell),隨著版本的不斷更新和語言新功能的添加,越來越多被用於獨立的、大型項目的開發。

Python還是一門解釋型的編程語言,Python也是面向對象的編程語言。對比其他編程語言來說,Python入門要簡單很多,更接近自然語言和正常思維方法,可以直接應用到工作中。同時當我們學習Python的時候非常容易上手,要比其他語言簡單很多,幾乎可以說是無所不能。

Python語言風格簡介:

Python在設計上堅持了清晰劃一的風格,這使得Python成為一門易讀、易維護,並且被大量用戶所歡迎的、用途廣泛的語言。

對於一個特定的問題,只要有一種最好的方法來解決就好。這在由Tim Peters寫的Python格言裡面表述為:There should be one-- and preferably only one --obvious way to do it. 這正好和Perl語言的中心思想TMTOWTDI完全相反。

Python的作者有意的設計限制性很強的語法,使得不好的編程習慣都不能通過編譯。其中很重要的一項就是Python的縮進規則。

『柒』 python2.4的openpyxl1.1.5的style類用法

單元格可以使用漸變色緩跡好填充,style.color.start_color是漸變色的起始色,end_color是結束色,如果兩個顏色相同,則單元格內就是一個顏色。就像下面這樣。

importopenpyxl

fromopenpyxl.styleimportColor,州逗Fill
fromopenpyxl.cellimportget_column_letter
..

cell=sheet.cell(column=pt[0],row=pt[1])
cell.style.fill.fill_type=Fill.FILL_SOLID
cell.style.fill.start_color=Color(color)
擾鉛cell.style.fill.end_color=Color(color)

『捌』 style是python-docx庫文檔對象嗎

不是。沒有任何辦法

『玖』 python爬取網頁數據怎樣去除style

用正式則表達式,把style屬性去除就好。

熱點內容
linux64位內存 發布:2025-07-16 01:02:36 瀏覽:960
壓縮衣尺碼表 發布:2025-07-16 00:47:33 瀏覽:640
安卓恢復了出廠怎麼找回照片 發布:2025-07-16 00:43:56 瀏覽:933
為什麼說伺服器已停止響應 發布:2025-07-16 00:29:36 瀏覽:391
python判斷字元串是否為空 發布:2025-07-16 00:21:47 瀏覽:210
安卓轉蘋果用什麼軟體 發布:2025-07-16 00:21:45 瀏覽:628
安卓官服如何登ios 發布:2025-07-16 00:21:32 瀏覽:624
天龍搶店腳本 發布:2025-07-16 00:14:47 瀏覽:958
華為榮耀存儲卡 發布:2025-07-16 00:10:40 瀏覽:659
mysql創建utf8資料庫 發布:2025-07-16 00:04:08 瀏覽:105