'How To save Pdf to Excel file in Django
I have a Doubt How to save pdf file to Excel File :
Here My Code Is :
I Have Pdf File Need to Extract Some text In The Pdf File to Excel ,
What Actually I needed Is Need to Upload Pdf File in Template And Download ,
Need to Render the Pdf File And Save Into My Database And Retrieve To Next template As Download File
Here My Code Is :
def index(request):
if request.method == 'POST':
form = MyfileUploadForm(request.POST, request.FILES)
if form.is_valid():
name = form.cleaned_data['file_name']
the_files = form.cleaned_data['files_data']
print(the_files)
my_dataframe = pd.DataFrame()
with pdfplumber.open(the_files) as pdf:
for page in pdf.pages:
text = page.extract_text().splitlines()
nameIdx, add1Idx, add2Idx, add3Idx, ShipmentIdx, WaybillIdx, phoneIdx = -1, -1, -1, -1, -1, -1, -1
print('vikram2')
for idx, line in enumerate(text):
if "Shipment Reference :" in line:
ShipmentIdx = idx
if "Waybill Number : " in line:
WaybillIdx = idx
if "Invoice Number :" in line:
nameIdx = idx - 1
if "Receiver :" in line:
add1Idx = idx + 4
if "Receiver :" in line:
add2Idx = idx + 6
if "Receiver :" in line:
add3Idx = idx + 7
if "Tax ID :" in line:
phoneIdx = idx - 1
textIdx = idx - 2
if "Shipment Reference : " in line:
phoneIdx2 = idx + 1
Shipment = text[ShipmentIdx] if ShipmentIdx != -1 else ''
WayBill = text[WaybillIdx] if WaybillIdx != -1 else ''
Name = text[nameIdx] if nameIdx != -1 else ''
add1 = text[add1Idx] if add1Idx != -1 else ''
add2 = text[add2Idx] if add2Idx != -1 else ''
add3 = text[add3Idx] if add3Idx != -1 else ''
phone1 = text[phoneIdx] if phoneIdx != -1 else ''
phone2 = text[phoneIdx2] if phoneIdx2 != -1 else ''
phone3 = text[textIdx] if textIdx != -1 else ''
address = add1 + ", " + add2 + ", " + add3
a = phone1
b = phone2
c = phone3
phone_number = a if (a==b) else c
order = Shipment.split('Shipment Reference : ')
order_id = order[1]
bill = WayBill.split('Waybill Number : ')
ship = bill[1]
customer = Name.split(', ')
customer_name = customer[1]
my_list = [order_id, ship, customer_name, address, phone_number]
my_list = pd.Series(my_list)
my_dataframe = my_dataframe.append(my_list, ignore_index=True)
my_dataframe = my_dataframe.rename(columns={'Shipment Reference': order_id, 'Waybill Number': ship, 'Name': customer_name, 'Address': address, 'phone': phone_number})
name2 = f'{str(name)}.xlsx'
name3 = my_dataframe.to_excel(name2)
print(name3)
file_upload(file_name=name, my_file=name3).save()
return render(request,"upload.html")
else:
return HttpResponse('error')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
