I'm using django. In the view I have created a function that passes a file to it and the function returns the number of pages and the type of file. Works correctly. Now I want to add that it can also count the pages of ".doc" but I have tried it in several ways and I can't. I have tried to do it with "win32com" but when installing it gives me an error. If it's not possible, how should I do it? Maybe first convert it to ".docx" and then count the pages? This is my function:
def count_pages(file_content, filename):
file_extension = os.path.splitext(filename)[1]
if file_extension.lower() == ".pdf":
pdf_reader = PyPDF2.PdfReader(file_content)
num_pages = len(pdf_reader.pages)
file_type = "PDF"
elif file_extension.lower() == ".docx":
text = docx2txt.process(BytesIO(file_content))
num_pages = text.count("\f") + 1
file_type = "Word"
elif file_extension.lower() == ".pptx":
prs = Presentation(BytesIO(file_content))
num_pages = len(prs.slides)
file_type = "PowerPoint"
elif file_extension.lower() == ".xlsx":
wb = load_workbook(BytesIO(file_content))
num_pages = len(wb.sheetnames)
file_type = "Excel"
else:
num_pages = 0
file_type = "Unknown"
return num_pages, file_typ