How do I format cells in Google Sheets using Python? [AttributeError: 'Worksheet' object has no attribute 'format']
How do I format cells in Google Sheets using Python? [AttributeError: 'Worksheet' object has no attribute 'format']
我正在构建一个网络抓取工具并尝试通过 Python 格式化 Google 表格中的一些数据。在下面的示例中,我能够将存储在变量中的数据发送到 Google 表格中的各个单元格。我如何使用 Python 格式化 Google 工作表中单个单元格或单元格范围的内容?
在我的代码中,我使用了 gspread
和 BeautifulSoup
库。为了在下面的示例中保持简单,假设数据已存储在以下变量中:title
、price
和 rating
。然后我将该数据发送到 Google 表格并尝试格式化单元格。
1 import gspread
2
3 # Set up access to Google Sheets, URL tail too long to display
4 gc = gspread.authorize(GoogleCredentials.get_application_default())
5 wb = gc.open_by_url('https://docs.google.com/spreadsheets/d/ ... ')
6 sheet = wb.worksheet('Sheet1')
7
8 # Store data into variables
9 title = "Flash Drive"
10 price = 20.00
11 rating = 4.6
12
13 # Send data to specific cells in Google Sheets (Cells: J8, K8, L8)
14 sheet.update_cell(8, 9, title)
15 sheet.update_cell(8, 10, price)
16 sheet.update_cell(8, 11, rating)
17
18 # Make bold the contents of J8 through L8 (3 cells across)
19 sheet.format('J8:L8', {'textFormat': {'bold': True}})
在代码中,J8、K8 和 L8 引用正在更新的 Google 工作表中的单元格。一切正常,直到 Line 19
出现以下错误。我该如何解决?我的代码中是否缺少某些内容?
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-26-477e46797660> in <module>()
18
19 # Make bold the contents of J8 through L8 (3 cells across)
---> 20 sheet.format('J8:L8', {'textFormat': {'bold': True}})
AttributeError: 'Worksheet' object has no attribute 'format'
另一方面,如果我在 运行 代码之前将 Google 工作表中那些单元格的内容加粗,则格式保留在 运行 代码之后。基本上,对于新的单元格内容,格式会保留以前的内容。如何使用 gspread
库自动更新格式?
试试这个:
from gspread_formatting import *
fmt = cellFormat(
textFormat=textFormat(bold=True)
)
format_cell_range(sheet, 'J8:L8', fmt)
并删除这一行:
sheet.format('J8:L8', {'textFormat': {'bold': True}})
您可以找到更多信息 here。
也替换这个:
sheet.update_cell(8, 9, title)
sheet.update_cell(8, 10, price)
sheet.update_cell(8, 11, rating)
有了这个:
sheet.update_cell(8, 10, title)
sheet.update_cell(8, 11, price)
sheet.update_cell(8, 12, rating)
因为J,K,L分别在第10,11,12列
我正在构建一个网络抓取工具并尝试通过 Python 格式化 Google 表格中的一些数据。在下面的示例中,我能够将存储在变量中的数据发送到 Google 表格中的各个单元格。我如何使用 Python 格式化 Google 工作表中单个单元格或单元格范围的内容?
在我的代码中,我使用了 gspread
和 BeautifulSoup
库。为了在下面的示例中保持简单,假设数据已存储在以下变量中:title
、price
和 rating
。然后我将该数据发送到 Google 表格并尝试格式化单元格。
1 import gspread
2
3 # Set up access to Google Sheets, URL tail too long to display
4 gc = gspread.authorize(GoogleCredentials.get_application_default())
5 wb = gc.open_by_url('https://docs.google.com/spreadsheets/d/ ... ')
6 sheet = wb.worksheet('Sheet1')
7
8 # Store data into variables
9 title = "Flash Drive"
10 price = 20.00
11 rating = 4.6
12
13 # Send data to specific cells in Google Sheets (Cells: J8, K8, L8)
14 sheet.update_cell(8, 9, title)
15 sheet.update_cell(8, 10, price)
16 sheet.update_cell(8, 11, rating)
17
18 # Make bold the contents of J8 through L8 (3 cells across)
19 sheet.format('J8:L8', {'textFormat': {'bold': True}})
在代码中,J8、K8 和 L8 引用正在更新的 Google 工作表中的单元格。一切正常,直到 Line 19
出现以下错误。我该如何解决?我的代码中是否缺少某些内容?
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-26-477e46797660> in <module>()
18
19 # Make bold the contents of J8 through L8 (3 cells across)
---> 20 sheet.format('J8:L8', {'textFormat': {'bold': True}})
AttributeError: 'Worksheet' object has no attribute 'format'
另一方面,如果我在 运行 代码之前将 Google 工作表中那些单元格的内容加粗,则格式保留在 运行 代码之后。基本上,对于新的单元格内容,格式会保留以前的内容。如何使用 gspread
库自动更新格式?
试试这个:
from gspread_formatting import *
fmt = cellFormat(
textFormat=textFormat(bold=True)
)
format_cell_range(sheet, 'J8:L8', fmt)
并删除这一行:
sheet.format('J8:L8', {'textFormat': {'bold': True}})
您可以找到更多信息 here。
也替换这个:
sheet.update_cell(8, 9, title)
sheet.update_cell(8, 10, price)
sheet.update_cell(8, 11, rating)
有了这个:
sheet.update_cell(8, 10, title)
sheet.update_cell(8, 11, price)
sheet.update_cell(8, 12, rating)
因为J,K,L分别在第10,11,12列