使用AWS Textract提取表格后,可以使用Python代码将包含逗号的整数拆分为另一列。以下是一个示例解决方法:
import boto3
# 初始化AWS Textract客户端
textract = boto3.client('textract')
# 调用AWS Textract提取表格
response = textract.start_document_analysis(
Document={
'S3Object': {
'Bucket': 'your-bucket-name',
'Name': 'your-image-file-name'
}
},
FeatureTypes=['TABLES']
)
# 获取分析结果
job_id = response['JobId']
result = textract.get_document_analysis(JobId=job_id)
# 解析表格
tables = result['Blocks']
for table in tables:
if table['BlockType'] == 'TABLE':
rows = table['Relationships'][0]['Ids']
for row in rows:
columns = result['Blocks'][row]['Relationships'][0]['Ids']
for column in columns:
cell = result['Blocks'][column]
if cell['BlockType'] == 'CELL':
# 检查单元格文本是否包含逗号
if ',' in cell['Text']:
# 拆分包含逗号的整数为两列
text = cell['Text']
split_text = text.split(',')
if len(split_text) == 2:
# 更新原单元格文本为不包含逗号的整数
cell['Text'] = split_text[0].strip()
# 创建新的单元格来存储逗号后的内容
new_cell = {
'BlockType': 'CELL',
'ColumnIndex': cell['ColumnIndex'] + 1,
'RowIndex': cell['RowIndex'],
'Text': split_text[1].strip()
}
# 将新单元格添加到结果中
result['Blocks'].append(new_cell)
# 打印更新后的表格
for block in result['Blocks']:
if block['BlockType'] == 'CELL':
print(block['Text'])
上述代码首先使用AWS Textract提取表格,然后遍历表格中的每个单元格。对于包含逗号的整数,它会将其拆分为两列并更新原单元格的文本,同时创建一个新的单元格来存储逗号后的内容。最后,它打印出更新后的表格。
请确保将your-bucket-name
和your-image-file-name
替换为实际的S3存储桶和图像文件名。此外,还需要适当配置AWS凭证和权限以访问Textract服务。