'how to open a text file after opening workbook in openpyxl Python
i have been trying to append some data on my excel sheet using openpyxl and after that append some data in my text file...for some reason it gives me error whenever i try to open my text file
import openpyxl
from openpyxl import*
path = "students.xlsx"
To open the workbook
wb = openpyxl.load_workbook(path)
Get workbook active sheet object
sheet = wb.active
data to later append
data = (
(1, "john", 1/5/2022)
)
appending data
sheet.append(data)
saving and closing xlsx file
wb.save("students.xlsx")
wb.close()
problem lies here
with open("students.txt", "a") as f:
f.write(data[0]+data[1] + data[2])
here's my error
Traceback (most recent call last):
File "D:\taha\tools\attendance\quiz.py", line 32, in <module>
with open("students.txt", "a") as f:
File "C:\Users\FLH\AppData\Local\Programs\Python\Python310\lib\site-packages\openpyxl\reader\excel.py", line 315, in load_workbook
reader = ExcelReader(filename, read_only, keep_vba,
File "C:\Users\FLH\AppData\Local\Programs\Python\Python310\lib\site-packages\openpyxl\reader\excel.py", line 124, in __init__
self.archive = _validate_archive(fn)
File "C:\Users\FLH\AppData\Local\Programs\Python\Python310\lib\site-packages\openpyxl\reader\excel.py", line 94, in _validate_archive
raise InvalidFileException(msg)
openpyxl.utils.exceptions.InvalidFileException: openpyxl does not support .txt file format, please check you can open it with Excel first. Supported formats are: .xlsx,.xlsm,.xltx,.xltm
Solution 1:[1]
In a quick view, you defined data as a tuple that contains a tuple with three elements.
As a first step to solve should use a list instead a tuple of tuples:
data = [1, "john", 1/5/2022]
Could you try this and let me know if it solves or you need more help.
Saying “thanks” is appreciated, but it doesn’t answer the question. Instead, vote up the answers that helped you the most! If these answers were helpful to you, please consider saying thank you in a more constructive way – by contributing your own answers to questions your peers have asked here.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | jpg997 |
