Python-docx是python操作word文件最常用的库,与表格相关的对象包含Table\Cell\Row\Column\Rows\Column。但所有的对象都只能指定行号和列号,但都没有行号和列号的属性,开始写了一段程序,想在一个4*4的表格里遍历单元格,内容填充为单元格的行号和列号:
from docx import Document
doc = Document()
table = doc.add_table(4,4)
for row in table.rows:
for cell in row.cells:
cell.text = '第{}行,第{}个格'.format(row,cell)
doc.save('test.docx')
运行的结果如下:
后来发现一个Python的内置枚举函数:enumerate()。enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中。
以下是 enumerate() 方法的语法:
enumerate(sequence, [start=0])
参数:
- sequence — 一个序列、迭代器或其他支持迭代对象。
- start — 下标起始位置的值。
返回值:
返回 enumerate(枚举) 对象。
修改代码如下:
for row,row_obj in enumerate(table.rows):
for cell,cell_obj in enumerate(row_obj.cells):
cell_obj.text = '第{}行,第{}个格'.format(row,cell)
doc.save('test.docx')
出来的结果如下:
如果把返回的enumerate对象转成list打印
print(list(enumerate(table.rows)))
会得到如下结果:
这个序号就可以取到行号了。至此问题解决。关键还是对python的内置函数不够熟悉,导致用第三方库的时候解决问题没法得心应手。所以学python首先要把内置函数吃透!
108 Responses
<strong>canadian pharmacy world https://pudbiascan.strikingly.com/</strong>
Amazing info. With thanks!
Canadian viagra https://essidi.cm/community/profile/canadianpharmacy/
You’ve made your point!
Cheap viagra https://ascenddeals.com/beaverage/profile/canadianpharmacy/
You made your stand pretty well!!
real viagra 100mg
Python-docx操作word中的表格获取行号和列号 – 白衣狼的杂食铺
best essay writing service
Python-docx操作word中的表格获取行号和列号 – 白衣狼的杂食铺
essay homework help online
Python-docx操作word中的表格获取行号和列号 – 白衣狼的杂食铺
best online essay writers
Python-docx操作word中的表格获取行号和列号 – 白衣狼的杂食铺
pay someone to write my essay
Python-docx操作word中的表格获取行号和列号 – 白衣狼的杂食铺
us essay writing service
Python-docx操作word中的表格获取行号和列号 – 白衣狼的杂食铺
how can i pay someone to write my essay
Python-docx操作word中的表格获取行号和列号 – 白衣狼的杂食铺
what is the best custom essay site
Python-docx操作word中的表格获取行号和列号 – 白衣狼的杂食铺
help writing my college essay
Python-docx操作word中的表格获取行号和列号 – 白衣狼的杂食铺
will someone write my essay for me
Python-docx操作word中的表格获取行号和列号 – 白衣狼的杂食铺
i need help writing a descriptive essay
Python-docx操作word中的表格获取行号和列号 – 白衣狼的杂食铺
write my essay for cheap
Python-docx操作word中的表格获取行号和列号 – 白衣狼的杂食铺
help with essay introduction
Python-docx操作word中的表格获取行号和列号 – 白衣狼的杂食铺
essay writing services
Python-docx操作word中的表格获取行号和列号 – 白衣狼的杂食铺
in an essay help you guide
Python-docx操作word中的表格获取行号和列号 – 白衣狼的杂食铺
us pharmacy phentermine
Python-docx操作word中的表格获取行号和列号 – 白衣狼的杂食铺
doxycycline online pharmacy no prescription
Python-docx操作word中的表格获取行号和列号 – 白衣狼的杂食铺
online pharmacy uk rohypnol
Python-docx操作word中的表格获取行号和列号 – 白衣狼的杂食铺
online pharmacy no prescription provigil
Python-docx操作word中的表格获取行号和列号 – 白衣狼的杂食铺
Micronase
Micronase
tadalafil herbal substitute
tadalafil herbal substitute
viagra daily use
viagra daily use
orignal cialis
orignal cialis
overnight cialis online
overnight cialis online
100mg sildenafil 30 tablets price
100mg sildenafil 30 tablets price
compare sildenafil prices
compare sildenafil prices
viagra in india cost
viagra in india cost
buy cialis without a prescription
buy cialis without a prescription
where can i order generic viagra
where can i order generic viagra
coupon for cialis 5mg
coupon for cialis 5mg
what’s the difference between sildenafil and tadalafil
what’s the difference between sildenafil and tadalafil
cialis pastilla
cialis pastilla
heb pharmacy
heb pharmacy
buy phentermine online pharmacy
buy phentermine online pharmacy
where can i buy sildenafil 100mg
where can i buy sildenafil 100mg
buying generic cialis online safe
buying generic cialis online safe
how long before cialis kicks in
how long before cialis kicks in
buy viagra online fast delivery
buy viagra online fast delivery
lortab 10 online pharmacy
lortab 10 online pharmacy
cialis lilly australia
cialis lilly australia
how much is sildenafil from canada
how much is sildenafil from canada
viagra for women 2013
viagra for women 2013
buy generic viagra online fast shipping
buy generic viagra online fast shipping
which is better cialis or levitra
which is better cialis or levitra
trimethoprim/sulfamethoxazole medicine
trimethoprim/sulfamethoxazole medicine
neurontin monotherapy
neurontin monotherapy
metronidazole herx
metronidazole herx
valtrex hallucinations
valtrex hallucinations
pregabalin vs gabapentin for sleep
pregabalin vs gabapentin for sleep
lisinopril azithromycin
lisinopril azithromycin
rybelsus 14 mg for weight loss
rybelsus 14 mg for weight loss
fluoxetine increased anxiety how long
fluoxetine increased anxiety how long
keflex and food
keflex and food
side effects to zoloft
side effects to zoloft
metronidazole monograph
metronidazole monograph
cephalexin 250 for dogs
cephalexin 250 for dogs
lexapro vs zoloft weight gain
lexapro vs zoloft weight gain
is duloxetine the same as cymbalta
is duloxetine the same as cymbalta
what is cephalexin used for in dogs
what is cephalexin used for in dogs
bactrim uti
bactrim uti
can amoxicillin treat chlamydia
can amoxicillin treat chlamydia
diclofenac over the counter
diclofenac over the counter
diltiazem and simvastatin
diltiazem and simvastatin
what diet works best with contrave
what diet works best with contrave
does flomax help kidney stones pass
does flomax help kidney stones pass
difference between escitaloprám and citalopram
difference between escitaloprám and citalopram
ezetimibe side effects uk
ezetimibe side effects uk
what is cozaar used for
what is cozaar used for
ddavp nasal solution
ddavp nasal solution
aspirin acetominophen
aspirin acetominophen
is amitriptyline
is amitriptyline
side effects of bupropion xl
side effects of bupropion xl
ashwagandha half life
ashwagandha half life
meloxicam vs celecoxib
meloxicam vs celecoxib
robaxin for dogs
robaxin for dogs
acarbose postprandial
acarbose postprandial
protonix drug interactions
protonix drug interactions
remeron dosages
remeron dosages
wet finger method effexor withdrawal reddit
wet finger method effexor withdrawal reddit
what is equal to voltaren gel
what is equal to voltaren gel
50 mg spironolactone for hirsutism
50 mg spironolactone for hirsutism
synthroid usage
synthroid usage
pharmacy online india
pharmacy online india
pastillas levitra
pastillas levitra
sildenafil 20 mg reviews
sildenafil 20 mg reviews
sildenafil 50 mg para que sirve
sildenafil 50 mg para que sirve
cialis online pills
cialis online pills
tadalafil dose
tadalafil dose
ivermectin 18mg
ivermectin 18mg
ivermectin 6mg
ivermectin 6mg
cialis generico tadalafil 10 pillole 20mg
cialis generico tadalafil 10 pillole 20mg
sildenafil tadalafil vardenafil
sildenafil tadalafil vardenafil
ivermectin lotion for lice
ivermectin lotion for lice
gabapentin 100mg for dogs
gabapentin 100mg for dogs
trimox ingredients
trimox ingredients
how to stop heart palpitations from prednisone
how to stop heart palpitations from prednisone
valacyclovir and prednisone
valacyclovir and prednisone
how long does nolvadex stay in your system
how long does nolvadex stay in your system
can you give dogs amoxicillin
can you give dogs amoxicillin
trazodone efectos secundarios
trazodone efectos secundarios
pregabalin and gabapentin
pregabalin and gabapentin
side effects of viagra in older men
side effects of viagra in older men
percocet online pharmacy no prescription
percocet online pharmacy no prescription
sildenafil and alcohol
sildenafil and alcohol
sildenafil 20mg
sildenafil 20mg