【发布时间】:2023-02-25 02:23:01
【问题描述】:
我一直在尝试在 python 中创建一个应用程序,您可以在其中为文本选择字体,为此我有一个 excel,其中我有 347 个单元格,每个单元格都有一个字体名称。问题是我必须提取每个单元格的信息并将其存储在一个变量中,这非常慢。
为此,我使用了以下代码:
# Excel file with all the fonts
fonts_excel = 'Fonts.xlsx'
workbook = load_workbook(fonts_excel) # Load the entire workbook.
worksheet = workbook['Sheet1'] # Load the worksheet in which the fonts are on
all_rows = list(workbook.rows) # Load all the rows of thw worksheet
# Load all the cells
for cell in all_rows[1]:
font1 = cell.value # Cell 1
for cell in all_rows[2]:
font2 = cell.value # Cell 2
for cell in all_rows[3]:
font3 = cell.value # Cell 3
for cell in all_rows[4]:
font4 = cell.value # Cell 4
for cell in all_rows[5]:
font5 = cell.value # Cell 5
for cell in all_rows[6]:
font6 = cell.value # Cell 6
for cell in all_rows[7]:
font7 = cell.value # Cell 7
for cell in all_rows[8]:
font8 = cell.value # Cell 8
for cell in all_rows[9]:
font9 = cell.value # Cell 9
for cell in all_rows[10]:
font10 = cell.value # Cell 10
for cell in all_rows[11]:
font11 = cell.value # Cell 11
for cell in all_rows[12]:
font12 = cell.value # Cell 12
for cell in all_rows[13]:
font13 = cell.value # Cell 13
for cell in all_rows[14]:
font14 = cell.value # Cell 14
for cell in all_rows[15]:
font15 = cell.value # Cell 15
for cell in all_rows[16]:
font16 = cell.value # Cell 16
for cell in all_rows[17]:
font17 = cell.value # Cell 17
for cell in all_rows[18]:
font18 = cell.value # Cell 18
for cell in all_rows[19]:
font19 = cell.value # Cell 19
for cell in all_rows[20]:
font20 = cell.value # Cell 20
for cell in all_rows[21]:
font21 = cell.value # Cell 21
for cell in all_rows[22]:
font22 = cell.value # Cell 22
for cell in all_rows[23]:
font23 = cell.value # Cell 23
for cell in all_rows[24]:
font24 = cell.value # Cell 24
for cell in all_rows[25]:
font25 = cell.value # Cell 25
for cell in all_rows[26]:
font26 = cell.value # Cell 26
for cell in all_rows[27]:
font27 = cell.value # Cell 27
for cell in all_rows[28]:
font28 = cell.value # Cell 28
for cell in all_rows[29]:
font29 = cell.value # Cell 29
for cell in all_rows[30]:
font30 = cell.value # Cell 30
我想要的是能够放置 347 个源,而不必执行整个 for cell in all_rows[1]... 过程。这可能吗?谢谢。
【问题讨论】:
-
使用一个数组作为字体,然后使用一个循环来填充数组?
-
为什么要像
for cell in all_rows[10]一样遍历行,然后像font10 = cell.value一样将单元格值一遍又一遍地分配给同一个变量?这只是将all_rows[10]行的最后一个单元格值分配给该变量。也许这就是您最终想要的,但反复设置值然后覆盖它们是低效且没有意义的。 -
@DaveS 我该怎么做?我是 python 的新手,我真的不知道该怎么做。
-
@RandomDavis 我应该怎么做才能不这样做?
-
也许你应该解释一下你打算如何使用你稍后在代码中捕获的字体......否则很难知道你真正需要什么......
标签: python