Files
ExperionCrawler/dxf-graph/extract_pdf.py
2026-05-08 17:22:10 +09:00

103 lines
3.5 KiB
Python

#!/usr/bin/env python3
"""
PdfPig를 사용하여 plant-9100.pdf에서 모든 문자열을 추출하여 마크다운 파일로 저장
"""
import subprocess
import sys
# C# 코드 작성
csharp_code = '''
using System;
using System.Text;
using UglyToad.PdfPig;
using UglyToad.PdfPig.DocumentLayoutAnalysis.TextExtractor;
class Program
{
static void Main(string[] args)
{
string pdfPath = "/home/windpacer/projects/ExperionCrawler/futurePlan/plant-9100.pdf";
string markdownPath = "/home/windpacer/projects/ExperionCrawler/futurePlan/plant-9100-extracted.md";
Console.WriteLine($"PdfPig 버전: {typeof(PdfDocument).Assembly.GetName().Version}");
Console.WriteLine($"PDF 파일: {pdfPath}");
Console.WriteLine();
using (var document = PdfDocument.Open(pdfPath))
{
var sb = new StringBuilder();
sb.AppendLine("# plant-9100.pdf 추출 결과");
sb.AppendLine();
sb.AppendLine("## PDF 정보");
sb.AppendLine();
sb.AppendLine($"- **버전**: {document.Version}");
sb.AppendLine($"- **페이지 수**: {document.NumberOfPages}");
sb.AppendLine($"- **제목**: {document.Information.Title ?? \"(없음)\"}");
sb.AppendLine($"- **작성자**: {document.Information.Author ?? \"(없음)\"}");
sb.AppendLine($"- **생성 프로그램**: {document.Information.Producer ?? \"(없음)\"}");
sb.AppendLine($"- **생성일**: {document.Information.CreationDate ?? \"(없음)\"}");
sb.AppendLine();
foreach (var page in document.GetPages())
{
sb.AppendLine($"## 페이지 {page.Number}");
sb.AppendLine();
sb.AppendLine($"- **크기**: {page.Width} x {page.Height}");
sb.AppendLine();
string text = page.Text;
sb.AppendLine("### 추출 텍스트");
sb.AppendLine();
sb.AppendLine("```");
sb.AppendLine(text);
sb.AppendLine("```");
sb.AppendLine();
Console.WriteLine($"페이지 {page.Number} 추출 완료 ({text.Length}자)");
}
System.IO.File.WriteAllText(markdownPath, sb.ToString());
Console.WriteLine();
Console.WriteLine($"전체 추출 완료. 결과 저장: {markdownPath}");
}
}
}
'''
# C# 파일 저장
with open('futurePlan/extract_pdf.cs', 'w') as f:
f.write(csharp_code)
print("C# 코드 작성 완료: futurePlan/extract_pdf.cs")
# 프로젝트 파일 생성
project_code = '''<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="PdfPig" Version="0.1.9" />
</ItemGroup>
</Project>
'''
with open('futurePlan/extract_pdf.csproj', 'w') as f:
f.write(project_code)
print("프로젝트 파일 작성 완료: futurePlan/extract_pdf.csproj")
# 빌드 및 실행
print()
print("빌드 및 실행 중...")
result = subprocess.run(
['dotnet', 'run', '--project', 'futurePlan/extract_pdf.csproj', '--configuration', 'Release'],
cwd='/home/windpacer/projects/ExperionCrawler',
capture_output=True,
text=True
)
print(result.stdout)
if result.stderr:
print("STDERR:", result.stderr)