Python-docx操作word中的表格获取行号和列号

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')

运行的结果如下:

原因是row和cell都是object,而不是数字,但她们都没有行号列号的属性,怎么办?

后来发现一个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首先要把内置函数吃透!

You may also like...

108 Responses

  1. 2023-03-19

    <strong>canadian pharmacy world https://pudbiascan.strikingly.com/</strong&gt;

    Amazing info. With thanks!

  2. 2023-05-17

    Canadian viagra https://essidi.cm/community/profile/canadianpharmacy/

    You’ve made your point!

  3. 2023-05-18

    Cheap viagra https://ascenddeals.com/beaverage/profile/canadianpharmacy/

    You made your stand pretty well!!

  4. 2023-11-23

    real viagra 100mg

    Python-docx操作word中的表格获取行号和列号 – 白衣狼的杂食铺

  5. 2023-12-11

    best essay writing service

    Python-docx操作word中的表格获取行号和列号 – 白衣狼的杂食铺

  6. 2023-12-11

    essay homework help online

    Python-docx操作word中的表格获取行号和列号 – 白衣狼的杂食铺

  7. 2023-12-11

    best online essay writers

    Python-docx操作word中的表格获取行号和列号 – 白衣狼的杂食铺

  8. 2023-12-11

    pay someone to write my essay

    Python-docx操作word中的表格获取行号和列号 – 白衣狼的杂食铺

  9. 2023-12-12

    us essay writing service

    Python-docx操作word中的表格获取行号和列号 – 白衣狼的杂食铺

  10. 2023-12-12

    how can i pay someone to write my essay

    Python-docx操作word中的表格获取行号和列号 – 白衣狼的杂食铺

  11. 2023-12-13

    what is the best custom essay site

    Python-docx操作word中的表格获取行号和列号 – 白衣狼的杂食铺

  12. 2023-12-13

    help writing my college essay

    Python-docx操作word中的表格获取行号和列号 – 白衣狼的杂食铺

  13. 2023-12-13

    will someone write my essay for me

    Python-docx操作word中的表格获取行号和列号 – 白衣狼的杂食铺

  14. 2023-12-14

    i need help writing a descriptive essay

    Python-docx操作word中的表格获取行号和列号 – 白衣狼的杂食铺

  15. 2023-12-14

    write my essay for cheap

    Python-docx操作word中的表格获取行号和列号 – 白衣狼的杂食铺

  16. 2023-12-14

    help with essay introduction

    Python-docx操作word中的表格获取行号和列号 – 白衣狼的杂食铺

  17. 2023-12-14

    essay writing services

    Python-docx操作word中的表格获取行号和列号 – 白衣狼的杂食铺

  18. 2023-12-14

    in an essay help you guide

    Python-docx操作word中的表格获取行号和列号 – 白衣狼的杂食铺

  19. 2023-12-19

    us pharmacy phentermine

    Python-docx操作word中的表格获取行号和列号 – 白衣狼的杂食铺

  20. 2023-12-19

    doxycycline online pharmacy no prescription

    Python-docx操作word中的表格获取行号和列号 – 白衣狼的杂食铺

  21. 2023-12-19

    online pharmacy uk rohypnol

    Python-docx操作word中的表格获取行号和列号 – 白衣狼的杂食铺

  22. 2023-12-20

    online pharmacy no prescription provigil

    Python-docx操作word中的表格获取行号和列号 – 白衣狼的杂食铺

  23. 2023-12-20

    Micronase

    Micronase

  24. 2023-12-20

    tadalafil herbal substitute

    tadalafil herbal substitute

  25. 2023-12-20

    viagra daily use

    viagra daily use

  26. 2023-12-21

    orignal cialis

    orignal cialis

  27. 2023-12-22

    overnight cialis online

    overnight cialis online

  28. 2023-12-22

    100mg sildenafil 30 tablets price

    100mg sildenafil 30 tablets price

  29. 2023-12-22

    compare sildenafil prices

    compare sildenafil prices

  30. 2023-12-22

    viagra in india cost

    viagra in india cost

  31. 2023-12-22

    buy cialis without a prescription

    buy cialis without a prescription

  32. 2023-12-22

    where can i order generic viagra

    where can i order generic viagra

  33. 2023-12-22

    coupon for cialis 5mg

    coupon for cialis 5mg

  34. 2024-01-05

    what’s the difference between sildenafil and tadalafil

    what’s the difference between sildenafil and tadalafil

  35. 2024-01-05

    cialis pastilla

    cialis pastilla

  36. 2024-01-06

    heb pharmacy

    heb pharmacy

  37. 2024-01-06

    buy phentermine online pharmacy

    buy phentermine online pharmacy

  38. 2024-01-07

    where can i buy sildenafil 100mg

    where can i buy sildenafil 100mg

  39. 2024-01-07

    buying generic cialis online safe

    buying generic cialis online safe

  40. 2024-01-07

    how long before cialis kicks in

    how long before cialis kicks in

  41. 2024-01-07

    buy viagra online fast delivery

    buy viagra online fast delivery

  42. 2024-01-08

    lortab 10 online pharmacy

    lortab 10 online pharmacy

  43. 2024-01-08

    cialis lilly australia

    cialis lilly australia

  44. 2024-01-09

    how much is sildenafil from canada

    how much is sildenafil from canada

  45. 2024-01-18

    viagra for women 2013

    viagra for women 2013

  46. 2024-01-19

    buy generic viagra online fast shipping

    buy generic viagra online fast shipping

  47. 2024-01-21

    which is better cialis or levitra

    which is better cialis or levitra

  48. 2024-01-22

    trimethoprim/sulfamethoxazole medicine

    trimethoprim/sulfamethoxazole medicine

  49. 2024-01-22

    neurontin monotherapy

    neurontin monotherapy

  50. 2024-01-23

    metronidazole herx

    metronidazole herx

  51. 2024-01-23

    valtrex hallucinations

    valtrex hallucinations

  52. 2024-01-23

    pregabalin vs gabapentin for sleep

    pregabalin vs gabapentin for sleep

  53. 2024-01-24

    lisinopril azithromycin

    lisinopril azithromycin

  54. 2024-01-24

    rybelsus 14 mg for weight loss

    rybelsus 14 mg for weight loss

  55. 2024-02-07

    fluoxetine increased anxiety how long

    fluoxetine increased anxiety how long

  56. 2024-02-07

    keflex and food

    keflex and food

  57. 2024-02-08

    side effects to zoloft

    side effects to zoloft

  58. 2024-02-08

    metronidazole monograph

    metronidazole monograph

  59. 2024-02-09

    cephalexin 250 for dogs

    cephalexin 250 for dogs

  60. 2024-02-10

    lexapro vs zoloft weight gain

    lexapro vs zoloft weight gain

  61. 2024-02-11

    is duloxetine the same as cymbalta

    is duloxetine the same as cymbalta

  62. 2024-02-28

    what is cephalexin used for in dogs

    what is cephalexin used for in dogs

  63. 2024-02-28

    bactrim uti

    bactrim uti

  64. 2024-03-22

    can amoxicillin treat chlamydia

    can amoxicillin treat chlamydia

  65. 2024-03-22

    diclofenac over the counter

    diclofenac over the counter

  66. 2024-03-22

    diltiazem and simvastatin

    diltiazem and simvastatin

  67. 2024-03-22

    what diet works best with contrave

    what diet works best with contrave

  68. 2024-03-22

    does flomax help kidney stones pass

    does flomax help kidney stones pass

  69. 2024-03-22

    difference between escitaloprám and citalopram

    difference between escitaloprám and citalopram

  70. 2024-03-22

    ezetimibe side effects uk

    ezetimibe side effects uk

  71. 2024-03-22

    what is cozaar used for

    what is cozaar used for

  72. 2024-03-22

    ddavp nasal solution

    ddavp nasal solution

  73. 2024-03-24

    aspirin acetominophen

    aspirin acetominophen

  74. 2024-03-24

    is amitriptyline

    is amitriptyline

  75. 2024-03-28

    side effects of bupropion xl

    side effects of bupropion xl

  76. 2024-03-28

    ashwagandha half life

    ashwagandha half life

  77. 2024-03-29

    meloxicam vs celecoxib

    meloxicam vs celecoxib

  78. 2024-04-14

    robaxin for dogs

    robaxin for dogs

  79. 2024-04-14

    acarbose postprandial

    acarbose postprandial

  80. 2024-04-14

    protonix drug interactions

    protonix drug interactions

  81. 2024-04-14

    remeron dosages

    remeron dosages

  82. 2024-04-14

    wet finger method effexor withdrawal reddit

    wet finger method effexor withdrawal reddit

  83. 2024-04-19

    what is equal to voltaren gel

    what is equal to voltaren gel

  84. 2024-04-19

    50 mg spironolactone for hirsutism

    50 mg spironolactone for hirsutism

  85. 2024-04-19

    synthroid usage

    synthroid usage

  86. 2024-05-13

    pharmacy online india

    pharmacy online india

  87. 2024-05-13

    pastillas levitra

    pastillas levitra

  88. 2024-05-13

    sildenafil 20 mg reviews

    sildenafil 20 mg reviews

  89. 2024-05-13

    sildenafil 50 mg para que sirve

    sildenafil 50 mg para que sirve

  90. 2024-05-13

    cialis online pills

    cialis online pills

  91. 2024-05-13

    tadalafil dose

    tadalafil dose

  92. 2024-05-17

    ivermectin 18mg

    ivermectin 18mg

  93. 2024-05-17

    ivermectin 6mg

    ivermectin 6mg

  94. 2024-05-17

    cialis generico tadalafil 10 pillole 20mg

    cialis generico tadalafil 10 pillole 20mg

  95. 2024-05-17

    sildenafil tadalafil vardenafil

    sildenafil tadalafil vardenafil

  96. 2024-05-17

    ivermectin lotion for lice

    ivermectin lotion for lice

  97. 2024-06-26

    gabapentin 100mg for dogs

    gabapentin 100mg for dogs

  98. 2024-06-26

    trimox ingredients

    trimox ingredients

  99. 2024-06-26

    how to stop heart palpitations from prednisone

    how to stop heart palpitations from prednisone

  100. 2024-06-26

    valacyclovir and prednisone

    valacyclovir and prednisone

  101. 2024-06-26

    how long does nolvadex stay in your system

    how long does nolvadex stay in your system

  102. 2024-06-27

    can you give dogs amoxicillin

    can you give dogs amoxicillin

  103. 2024-06-27

    trazodone efectos secundarios

    trazodone efectos secundarios

  104. 2024-06-29

    pregabalin and gabapentin

    pregabalin and gabapentin

  105. 2024-12-20

    side effects of viagra in older men

    side effects of viagra in older men

  106. 2024-12-20

    percocet online pharmacy no prescription

    percocet online pharmacy no prescription

  107. 2024-12-21

    sildenafil and alcohol

    sildenafil and alcohol

  108. 2024-12-21

    sildenafil 20mg

    sildenafil 20mg

发表回复