0

我正在尝试从pdf中抓取一些表格数据并将其转换为 csv 文件。表头是垂直的,当我在csv中打开我的数据框时,只显示列名的最后一个字母。

有什么方法可以让我水平显示完整的标题名称?

import pandas as pd
import pdfplumber
import requests


def download_file(url):
    local_filename = url.split("/")[-1]

    with requests.get(url) as r:
        with open(local_filename, "wb") as f:
            f.write(r.content)
    return local_filename


ap_url = "https://www.buckeye.com/Portals/0/Shipperbook/Section%201%20-%20Tariffs%207-1-21.pdf"
ap = download_file(ap_url)

with pdfplumber.open(ap) as pdf:
    page = pdf.pages[2]
    table = page.extract_table(
        table_settings={
            "vertical_strategy": "lines",
            "horizontal_strategy": "lines",
            "snap_tolerance": 4,
        }
    )

    df = pd.DataFrame(table, columns=table[0]).T.to_csv("output.csv")
4

0 回答 0