Python-尝试从另一个文件调用函数(使用python-pptx)

时间:2019-02-08 11:06:09

标签: python python-3.x python-pptx

我有一个构建Python脚本,该脚本使用python-pptx模块生成PowerPoint卡片组。由于各种格式,该代码运行了300多行代码。因此,我计划将其拆分为几个子功能,以便将来轻松维护。

以下是我尝试过的示例:

创建如下名为sub_func()的子功能

def sub_func():
    # create presentation with 1 slide ------
    prs = Presentation()
    slide = prs.slides.add_slide(prs.slide_layouts[5])

    # define chart data ---------------------
    chart_data = CategoryChartData()
    chart_data.categories = ['Zone1', 'Zone2', 'Zone3']
    chart_data.add_series('Series 1', (19.2, 21.4, 16.7))

    # add chart to slide --------------------
    x, y, cx, cy = Inches(2), Inches(2), Inches(6), Inches(4.5)
    slide.shapes.add_chart(
XL_CHART_TYPE.COLUMN_CLUSTERED, x, y, cx, cy, chart_data
    )
    prs.save('chart-00.pptx')

将以上内容保存到sub_func.py

下面是我的主要功能:

from pptx import Presentation
from pptx.chart.data import CategoryChartData
from pptx.enum.chart import XL_CHART_TYPE
from pptx.util import Inches
from sub_func import sub_func

def func():
    # create presentation with 1 slide ------
    prs = Presentation()
    slide = prs.slides.add_slide(prs.slide_layouts[5])

    # define chart data ---------------------
    chart_data = CategoryChartData()
    chart_data.categories = ['East', 'West', 'Midwest']
    chart_data.add_series('Series 1', (19.2, 21.4, 16.7))

    # add chart to slide --------------------
    x, y, cx, cy = Inches(2), Inches(2), Inches(6), Inches(4.5)
    slide.shapes.add_chart(
XL_CHART_TYPE.COLUMN_CLUSTERED, x, y, cx, cy, chart_data
    )
    ##Calling the sub-function below

    sub_func()

    prs.save('chart-01.pptx')

以上内容会生成一个PowerPoint文件,但不会执行该子功能。任何人都可以建议我哪里出问题了。

已编辑代码:

from pptx import Presentation
from pptx.chart.data import CategoryChartData
from pptx.enum.chart import XL_CHART_TYPE
from pptx.util import Inches
from subfolder import func1
from pptx.parts.chart import ChartPart
from pptx.parts.embeddedpackage import EmbeddedXlsxPart

def func():
    # create presentation with 1 slide ------
    prs = Presentation()
    slide = prs.slides.add_slide(prs.slide_layouts[5])

    # define chart data ---------------------
    chart_data = CategoryChartData()
    chart_data.categories = ['East', 'West', 'Midwest']
    chart_data.add_series('Series 1', (19.2, 21.4, 16.7))

    # add chart to slide --------------------
    x, y, cx, cy = Inches(2), Inches(2), Inches(6), Inches(4.5)
    slide.shapes.add_chart(XL_CHART_TYPE.COLUMN_CLUSTERED, x, y, cx, cy,chart_data)

    ##Calling the sub-function below

    def Add_Slide(self):
        xml_slides = prs.slides._sldIdLst
        xml_slides1 = prs1.slides._sldIdLst
        slides = list(xml_slides)
        slides1 = list(xml_slides1)
        xml_slides.append(xml_slides1)

    prs.save('chart-01.pptx')

以下是子功能:

from pptx import Presentation
from pptx.chart.data import CategoryChartData
from pptx.enum.chart import XL_CHART_TYPE
from pptx.util import Inches


def func1():
    # create presentation with 1 slide ------

    prs1 = Presentation()
    slide = prs1.slides.add_slide(prs1.slide_layouts[5])

    # define chart data ---------------------
    chart_data = CategoryChartData()
    chart_data.categories = ['East', 'West', 'Midwest']
    chart_data.add_series('Series 1', (100.2, 21.4, 16.7))

    # add chart to slide --------------------
    x, y, cx, cy = Inches(2), Inches(2), Inches(6), Inches(4.5)
    slide.shapes.add_chart(XL_CHART_TYPE.COLUMN_CLUSTERED, x, y, cx, cy,chart_data)

使用上面编辑的代码,我看到主要功能得到执行,但子功能没有得到执行,是因为它没有子功能的prs.save。能否请你指教..

3 个答案:

答案 0 :(得分:3)

sub_func.py是否在您的python路径中?如果它只是在您的工作目录中,则很可能导致ModuleNotFoundError。您需要先使用relative import

from .sub_func import sub_func

如果这不是问题,而仅仅是无作用的事实,那是因为您没有告诉它有作用。它创建带有1张幻灯片的演示文稿,但不保存。它不会被方法返回,并且返回值不会被变量捕获

答案 1 :(得分:0)

我不认为这是由于import语句引起的。 您将在sub_module中再次重新初始化演示对象。

#[derive(Debug)]
struct Column {
    header: String,
    amount: i32,
}

fn main() {
    let mut spreadsheet: Vec<Column> = Vec::new();

    spreadsheet.push(Column {
        header: "Car".to_string(),
        amount: 30300,
    });
    spreadsheet.push(Column {
        header: "House".to_string(),
        amount: 210800,
    });
    spreadsheet.push(Column {
        header: "Total".to_string(),
        amount: 0,
    });

    for column in &mut spreadsheet {
        //mutable borrow here
        if column.header == "Total" {
            column.amount = spreadsheet[0].amount //immutable borrow here
                        + spreadsheet[1].amount;
        } else {
            column.amount -= 300;
        }
    }

    for column in spreadsheet {
        println!("{:?}", column);
    }
}

您可以像下面一样将prs = Presentation() 对象传递给prs并尝试解决问题

主要功能

sub_func

和在sub_func中进行如下更改

from pptx import Presentation
from pptx.chart.data import CategoryChartData
from pptx.enum.chart import XL_CHART_TYPE
from pptx.util import Inches
from sub_func import sub_func

def func():
    # create presentation with 1 slide ------
    prs = Presentation()
    slide = prs.slides.add_slide(prs.slide_layouts[5])

    # define chart data ---------------------
    chart_data = CategoryChartData()
    chart_data.categories = ['East', 'West', 'Midwest']
    chart_data.add_series('Series 1', (19.2, 21.4, 16.7))

    # add chart to slide --------------------
    x, y, cx, cy = Inches(2), Inches(2), Inches(6), Inches(4.5)
    slide.shapes.add_chart(XL_CHART_TYPE.COLUMN_CLUSTERED, x, y, cx, cy,chart_data)

    ##Calling the sub-function below

    sub_func(prs)

    prs.save('chart-01.pptx')

答案 2 :(得分:-1)

from .sub_func import sub_func