Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
|
| 3 |
+
import fitz # PyMuPDF
|
| 4 |
+
import pandas as pd
|
| 5 |
+
|
| 6 |
+
# Define function to extract table data from PDF
|
| 7 |
+
def extract_data_from_pdf(pdf_path, start_pos, end_pos):
|
| 8 |
+
doc = fitz.open(pdf_path)
|
| 9 |
+
data = []
|
| 10 |
+
|
| 11 |
+
for page_num in range(len(doc)):
|
| 12 |
+
page = doc[page_num]
|
| 13 |
+
text = page.get_text("text")
|
| 14 |
+
lines = text.split('\n')
|
| 15 |
+
|
| 16 |
+
for line in lines:
|
| 17 |
+
# Check if line starts with the required 'Pos.' format
|
| 18 |
+
if line.startswith("Pos."):
|
| 19 |
+
parts = line.split()
|
| 20 |
+
pos = int(parts[1])
|
| 21 |
+
|
| 22 |
+
# Check if position is within the desired range
|
| 23 |
+
if start_pos <= pos <= end_pos:
|
| 24 |
+
item_code = parts[2]
|
| 25 |
+
unit = parts[3]
|
| 26 |
+
delivery_date = parts[4]
|
| 27 |
+
quantity = parts[5]
|
| 28 |
+
basic_price = parts[6]
|
| 29 |
+
discount = parts[7]
|
| 30 |
+
currency = parts[8]
|
| 31 |
+
amount = parts[9]
|
| 32 |
+
|
| 33 |
+
# Append structured data to list
|
| 34 |
+
data.append({
|
| 35 |
+
"Sl No": pos,
|
| 36 |
+
"Item Code": item_code,
|
| 37 |
+
"Unit": unit,
|
| 38 |
+
"Delivery Date": delivery_date,
|
| 39 |
+
"Quantity": quantity,
|
| 40 |
+
"Basic Price": basic_price,
|
| 41 |
+
"Discount": discount,
|
| 42 |
+
"Currency": currency,
|
| 43 |
+
"Amount": amount
|
| 44 |
+
})
|
| 45 |
+
|
| 46 |
+
# Convert list to DataFrame
|
| 47 |
+
df = pd.DataFrame(data)
|
| 48 |
+
return df
|
| 49 |
+
|
| 50 |
+
# Main function to run extraction and save to Excel
|
| 51 |
+
def main():
|
| 52 |
+
pdf_path = "P24101337 - LABELS.pdf" # Replace with your PDF file path
|
| 53 |
+
start_pos = 10
|
| 54 |
+
end_pos = 450
|
| 55 |
+
|
| 56 |
+
# Extract data from PDF
|
| 57 |
+
df = extract_data_from_pdf(pdf_path, start_pos, end_pos)
|
| 58 |
+
|
| 59 |
+
# Save DataFrame to Excel
|
| 60 |
+
output_path = "extracted_data.xlsx"
|
| 61 |
+
df.to_excel(output_path, index=False)
|
| 62 |
+
print(f"Data has been extracted and saved to {output_path}")
|
| 63 |
+
|
| 64 |
+
if __name__ == "__main__":
|
| 65 |
+
main()
|