如何使用Python或R将Excel中具有替代文本的图形导出为PDF?

Sam*_*m S 16 python pdf excel vba r

我有大约500个使用VBA在Excel中生成的图形,我需要将它们导出为pdf。这些图具有替代文本,以使盲人可以访问它们。当我使用VBA(ExportAsFixedFormat)生成pdf时,pdf中将缺少替代文本。python或R中是否有代码将图形从excel转换为pdf并保留替代文本?

如果我手动将图形另存为pdf,则替代文本将与图形一起保存在pdf文件中。但是,由于我的图表太多,因此能够自动执行此操作会很好。

 ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PdfFileName, _
        Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
Run Code Online (Sandbox Code Playgroud)

VBA中的上述代码对于创建pdf很有帮助,但不会保留替代文本。

EEM*_*EEM 11

以下代码为中的每个Sheet(不包括Worksheet)生成一个pdf文件ThisWorkbook

Sub Charts_Export()
Const kPath As String = "D:\@D_Trash\SO Questions\Output\#Name.pdf"    'Update as required
Dim oSht As Object, sPath As String
    With ThisWorkbook
        For Each oSht In .Sheets
            With oSht
                If oSht.Type <> xlWorksheet Then
                    sPath = Replace(kPath, "#Name", .Name)    'Update as required
                    .ExportAsFixedFormat _
                        Type:=xlTypePDF, _
                        Filename:=sPath, _
                        Quality:=xlQualityStandard, _
                        IncludeDocProperties:=True, _
                        IgnorePrintAreas:=False, _
                        OpenAfterPublish:=False
    End If: End With: Next: End With

    End Sub
Run Code Online (Sandbox Code Playgroud)

打开pdf文件后,同时按Shift+ Ctrl+ Y激活Read Out Loudpdf中的选项。然后同时按Shift+ Ctrl+ V阅读AlternativeText

在此处输入图片说明

之前的代码使用了OP发布的同一段代码,将图表导出为pdf文件,包括Alternative text每个pdf文件。

这似乎表明,这个问题可能是由于用于添加方法AlternativeTextChart。我找不到在AlternativeText将a Chart移至a时将其添加到a 的方法Sheet,因此,当the 仍是工作表中的一个对象()时AlternativeText,必须在将Charta移至a 之前添加。SheetChartShape

在将其移动到工作表之前,使用此方法将其添加AlternativeText到每个Chart页面。

Private Sub Charts_Add_AlternativeText()
Const kAltTxt As String = "This is a test of the Alt Text in graph [#Name]"    'Update as required
Dim ws As Worksheet
Dim co As ChartObject

    Set ws = ThisWorkbook.Worksheets("DATA")    'Update as required
    For Each co In ws.ChartObjects
        co.ShapeRange.AlternativeText = Replace(kAltTxt, "#Name", co.Name)    'Update as required
    Next

    End Sub
Run Code Online (Sandbox Code Playgroud)

或使用此方法将添加AlternativeText到每个工作Chart表。

Private Sub Charts_Add_AlternativeText()
Const kWsName As String = "!Temp"
Const kAltTxt As String = "This is a test of the Alt Text in graph [#Name]"     'Update as required
Dim wb As Workbook, ws As Worksheet
Dim oSht As Object, sp As Shape
Dim sChName As String, bIdx As Byte

    With Application
        .EnableEvents = False
        .DisplayAlerts = False
        .ScreenUpdating = False
        .Application.Calculation = xlCalculationManual
    End With

    Set wb = ThisWorkbook
    With wb

        Rem Add Temp Worksheet
        On Error Resume Next
        .Worksheets(kWsName).Delete
        On Error GoTo 0
        Set ws = .Worksheets.Add(After:=.Sheets(.Sheets.Count))
        ws.Name = kWsName

        Rem Work with Chart Sheets
        For Each oSht In .Sheets
            With oSht
                If oSht.Type <> xlWorksheet Then

                    Rem Move Chart to Temp Worksheet
                    bIdx = .Index
                    sChName = .Name
                    .Location Where:=xlLocationAsObject, Name:=kWsName

                    Set sp = ws.Shapes(1)
                    With sp

                        Rem Add AlternativeText to Shape (Chart)
                        .AlternativeText = Replace(kAltTxt, "#Name", sChName)    'Update as required

                        Rem Move Chart to Chart Sheet
                        .Chart.Location Where:=xlLocationAsNewSheet, Name:=sChName
                        wb.Sheets(sChName).Move Before:=wb.Sheets(bIdx)

    End With: End If: End With: Next: End With

    With Application
        .EnableEvents = True
        .DisplayAlerts = True
        .ScreenUpdating = True
        .Application.Calculation = xlCalculationAutomatic
    End With

End Sub
Run Code Online (Sandbox Code Playgroud)