前提・実現したいこと
Excelの大量のデータをコピーしスプレッドシートの特定の場所に張り付けたい
発生している問題・エラーメッセージ
Traceback (most recent call last):
File "c:/Users/y-shimoda/Desktop/python 試し/copy01.py", line 29, in <module>
wks.update_cell(row=1,col=2,value=copy)
File "C:\Users\y-shimoda\AppData\Local\Programs\Python\Python38\lib\site-packages\gspread\models.py", line
data = self.spreadsheet.values_update(
File "C:\Users\y-shimoda\AppData\Local\Programs\Python\Python38\lib\site-packages\gspread\models.py", line
r = self.client.request('put', url, params=params, json=body)
File "C:\Users\y-shimoda\AppData\Local\Programs\Python\Python38\lib\site-packages\gspread\client.py", line
response = getattr(self.session, method)(
File "C:\Users\y-shimoda\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\sessions.py", li
return self.request('PUT', url, data=data, **kwargs)
File "C:\Users\y-shimoda\AppData\Local\Programs\Python\Python38\lib\site-packages\google\auth\transport\req
response = super(AuthorizedSession, self).request(
File "C:\Users\y-shimoda\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\sessions.py", li
prep = self.prepare_request(req)
File "C:\Users\y-shimoda\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\sessions.py", li
p.prepare(
File "C:\Users\y-shimoda\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\models.py", line
self.prepare_body(data, files, json)
File "C:\Users\y-shimoda\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\models.py", line
body = complexjson.dumps(json)
File "C:\Users\y-shimoda\AppData\Local\Programs\Python\Python38\lib\site-packages\simplejson_init_.py",
return _default_encoder.encode(obj)
File "C:\Users\y-shimoda\AppData\Local\Programs\Python\Python38\lib\site-packages\simplejson\encoder.py", l
chunks = list(chunks)
File "C:\Users\y-shimoda\AppData\Local\Programs\Python\Python38\lib\site-packages\simplejson\encoder.py", l
for chunk in _iterencode_dict(o, _current_indent_level):
File "C:\Users\y-shimoda\AppData\Local\Programs\Python\Python38\lib\site-packages\simplejson\encoder.py", l
for chunk in chunks:
File "C:\Users\y-shimoda\AppData\Local\Programs\Python\Python38\lib\site-packages\simplejson\encoder.py", l
o = _default(o)
File "C:\Users\y-shimoda\AppData\Local\Programs\Python\Python38\lib\site-packages\simplejson\encoder.py", line 272, in default
raise TypeError('Object of type %s is not JSON serializable' %
TypeError: Object of type Cell is not JSON serializable
コード挿入
python
1import gspread 2 3 4from oauth2client.service_account import ServiceAccountCredentials 5 6scope = ['https://spreadsheets.google.com/feeds', 7 'https://www.googleapis.com/auth/drive'] 8 9#スプレッドシート選択 10credentials = ServiceAccountCredentials.from_json_keyfile_name('2020.json', scope) 11gc = gspread.authorize(credentials) 12wks = gc.open('gspreadサンプル').sheet1 13 14 15 16 17from openpyxl import load_workbook 18import pandas as pd 19import glob 20 21#コピーするワークシート選択 22wb=load_workbook('管理表 (5).xlsx') 23ws1=wb['id'] 24 25#セル指定 最後の行列 26for row in ws1.iter_rows(min_row=1,max_col=ws1.max_column,max_row=ws1.max_row): 27 for cell in row: 28 29 #コピー先指定 30 copy=cell 31 32wks.update_cell(row=1,col=2,value=copy) 33print(wks.cell('row=1,col=2')) 34
試したこと
pythonと連携させてexcelからスプレッドシートにとってくる方法を選びました
しかしpythonからexcel操作、pythonからスプレッドシートならうまく動くのですがまとめるとどうもうまく作動しません.
for row in ws1.iter_rows(min_row=1,max_col=ws1.max_column,max_row=ws1.max_row):を選んだのは毎回中のデータの量がかわり固定できないからです。
補足情報(FW/ツールのバージョンなど)
Package Version
astroid 2.4.2
cachetools 4.1.1
certifi 2020.6.20
chardet 3.0.4
colorama 0.4.3
comtypes 1.1.7
et-xmlfile 1.0.1
google-auth 1.22.1
google-auth-oauthlib 0.4.1
gspread 3.6.0
httplib2 0.18.1
idna 2.10
isort 5.5.3
jdcal 1.4.1
lazy-object-proxy 1.4.3
mccabe 0.6.1
numpy 1.19.2
oauth2client 4.1.3
oauthlib 3.1.0
openpyxl 3.0.5
pandas 1.1.3
pip 20.2.4
pyasn1 0.4.8
pyasn1-modules 0.2.8
pylint 2.6.0
python-dateutil 2.8.1
pytz 2020.1
pywin32 228
requests 2.24.0
requests-oauthlib 1.3.0
rsa 4.6
setuptools 49.2.1
simplejson 3.17.2
six 1.15.0
toml 0.10.1
urllib3 1.25.10
wrapt 1.12.1
xlwings 0.20.7
python 3.8.6
回答1件
あなたの回答
tips
プレビュー