from
1import datetime 2 3filepath = 'C:\\Users\\matar\\Documents\\book1-1.xlsx' 4wb = load_workbook(filename=filepath) 5ws1 = wb['データ'] 6ws2 = wb['集計'] 7 8startdate=datetime.datetime(int(ws2['B2'].value) , int(ws2['C2'].value) , int(ws2['D2'].value)) 9enddate=datetime.datetime(int(ws2['B3'].value) , int(ws2['C3'].value) , int(ws2['D3'].value)) 10 11lastrow1=ws1.max_row 12lastrow2=ws2.max_row 13lastcol2=ws2.max_column 14 15values1=[[cell.value for cell in row1] for row1 in ws1] 16 17for i in range(7, lastrow2+1): 18 for j in range(2, lastcol2+1): 19 counter = 0 20 21for k in range(1, lastrow1): 22 if values1[k][1] == ws2.cell(row=i, column=1).value: 23 if values1[k][2] == ws2.cell(row=6, column=j).value: 24 torihikidate = values1[k][3] 25 if startdate <= torihikidate <= enddate: 26 kingaku=values1[k][4] 27 counter = counter + int(kingaku) 28 29if counter is None: 30 counter=0 31 32ws2.cell(row=i, column=j).value = counter 33 34newfilepath = 'C:\\Users\\matar\\Documents\\book2-1.xlsx' 35wb.save(newfilepath) 36 37コード
Pythonでこのようなプログラムを作りました。Excelのデータを読み込み集計し別で作ってあるExcelの表に出力するプログラムを作ったつもりです。このプログラムを作った際に色々なエラーが出て修正したのですが下のエラーだけはどう直せばいいのか分かりません。
OSError Traceback (most recent call last)
Input In [1], in <cell line: 5>()
2 import datetime
4 filepath = 'C:\Users\matar\Documents\book1-1.xlsx'
----> 5 wb = load_workbook(filename=filepath)
6 ws1 = wb['データ']
7 ws2 = wb['集計']
File ~\anaconda3\envs\kadai\lib\site-packages\openpyxl\reader\excel.py:315, in load_workbook(filename, read_only, keep_vba, data_only, keep_links)
288 def load_workbook(filename, read_only=False, keep_vba=KEEP_VBA,
289 data_only=False, keep_links=True):
290 """Open the given filename and return the workbook
291
292 :param filename: the path to open or a file-like object
(...)
313
314 """
--> 315 reader = ExcelReader(filename, read_only, keep_vba,
316 data_only, keep_links)
317 reader.read()
318 return reader.wb
File ~\anaconda3\envs\kadai\lib\site-packages\openpyxl\reader\excel.py:124, in ExcelReader.init(self, fn, read_only, keep_vba, data_only, keep_links)
122 def init(self, fn, read_only=False, keep_vba=KEEP_VBA,
123 data_only=False, keep_links=True):
--> 124 self.archive = _validate_archive(fn)
125 self.valid_files = self.archive.namelist()
126 self.read_only = read_only
File ~\anaconda3\envs\kadai\lib\site-packages\openpyxl\reader\excel.py:96, in _validate_archive(filename)
89 msg = ('openpyxl does not support %s file format, '
90 'please check you can open '
91 'it with Excel first. '
92 'Supported formats are: %s') % (file_format,
93 ','.join(SUPPORTED_FORMATS))
94 raise InvalidFileException(msg)
---> 96 archive = ZipFile(filename, 'r')
97 return archive
File ~\anaconda3\envs\kadai\lib\zipfile.py:1248, in ZipFile.init(self, file, mode, compression, allowZip64, compresslevel, strict_timestamps)
1246 while True:
1247 try:
-> 1248 self.fp = io.open(file, filemode)
1249 except OSError:
1250 if filemode in modeDict:
OSError: [Errno 22] Invalid argument: '\u202aC:\Users\matar\Documents\book1-1.xlsx'
自分で調べた限りopen関数を使っているときのOSerrorはファイルの読み込みを失敗している?のかと思いました。なのでこのエラーに対してどう修正をすればいいのか教えてほしいです。

回答2件
あなたの回答
tips
プレビュー